Url manipulation with mod_rewrite and php-catcher for beginners

Url manipulation is one of the hottest areas on the new generation web (2.0). It provides more security and flexibility. It is user and search-engine friendly. The most popular combination is Apache-webserver + mod_rewrite + server-side catcher(mostly a php-file). In this tutorial we use very short examples to make it easy for newbies.

Why url manipulation & mod_rewrite?

  1. more security: hide file location
  2. shorter urls without file extensions like .html
  3. easy to remember / recommend urls
  4. search engine friendly

Redirect webpages with HTML, PHP, .htaccess, Java+Script, CGI-Perl, ASP.NET and ColdFusion

Here are examples of code how to redirect an old page/url/domain to a new one with most popular client+server side programming/scripting languages. We use 301 http status code (permanent redirect). If you need temporary redirection use 302 status code instead of 301. It is better to prefer server-side redirection, because some clients are surfing with javascript and http-refresh disabled browsers. Do not forget to test your code first!

.htacces and Mod_Rewrite (Apache Webserver)

Set auto_prepend_file with .htaccess on Hosteurope managed hosting

To set a standart auto_prepend_file for php on a dedicated root server is not a bit thing but on a virtual webhosting or managed server it could be a big issue. Because you dont have systemwide global access to httpd.conf and php.ini files.

My small solutions for Hostrurope managed virtual hosting packets:

Add this line to your .htaccess file
php_value auto_prepend_file  /is/htdocs/wp1193698874_ABCDEFGH/www/auto_prepend.php

And be sure that your auto_prepend.php really exists and its path is correct.

Prevent hotlinking with htaccess and mod_rewrite

To stopping content stealing, just create an .htaccess file (or add to the existing file) and paste code below and upload/save it to your main directory(root of your homepage).

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com(/)?.*$     [NC]
RewriteRule \.(bmp|gif|jpg|jpeg|png|mp3|swf|avi)$ - [NC,F,L]

You can replace requested files with a ' don't hotlink' image file.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com(/)?.*$     [NC]

.htaccess examples

redirect url with parameters

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} url_get_parameter_1=(.*)&url_get_parameter_2=(.*)
RewriteRule ^your/old/directory/file$ your/new/path/%1/%2 [R=301,L]

This code redirects :




Force files to download (not be displayed in client/browser)

Syndicate content

CafeWebmaster.com(CW) is a free online community for webdevelopers and beginners. Anybody can share their code, articles, tips, tutorials, code-examples or other webdesign related material on the site. Newbies can submit their questions and reply to existing questions. CW does not guarantee or warrant reliability of code, data and information published on the site. Use the site on your own risk. The site takes no responsibility of direct or indirect loss or any kind of harm to its users. The site also doesn't take responsibility of infected files or source code with any kind of infection or viruses, worms, spywares, malwares, trojan horses. CW reserves the right to edit, move, or delete any of content for any reason.