April 30, 2010 0

.htaccess apache rewrite rules

By in Server

Apache web server is a very popular and capable server used for serving up webpages like the one you are viewing. Apache uses what’s called .htaccess files in order to make configuration changes per directory.  For example , you have a directory you want to keep all images hidden from public view you would do this with an .htaccess file in that directory. If you wanted to redirect people from an old file that no longer exists to a new file you would create a rewrite rule within an .htaccess file.

If you have access to apaches main configuration file , that’s where you would make any changes that affect the whole site. If you are running multiple virtual hosts on a server you would create a separate apache configuration file for each domain.

When starting a rewrite rule you should begin with:


Options +FollowSymLinks
RewriteEngine On
RewriteBase /

Force www for a domain

It forces myDomain.com => www.myDomain.com

RewriteCond %{HTTP_HOST} !^www.myDomain.com$ [NC]
RewriteRule ^(.*)$ http://www.myDomain.com/$1 [L,R=301]

Removes the www for a domain

It forces www.myDomain.com => myDomain.com

RewriteCond %{HTTP_HOST} !^myDomain.com$ [NC]
RewriteRule ^(.*)$ http://myDomain.com/$1 [R=301,L]

Protect your content from being hot-linked from other sites

If another site is trying to access your files it will simply redirect the request to your domain. This will not allow any file to be shared. The ip address used on the fourth line is your server’s ip address If you do not know what that is you can remove that line.

RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^http://myDomain.com [NC]
RewriteCond %{HTTP_REFERER} !^http://www.myDomain.com [NC]
RewriteCond %{HTTP_REFERER} !^http://111.111.111.111 [NC]
RewriteRule ^.*$ http://www.myDomain.com/ [R,L]

Protect only certain file types

Protect only want to block SWF, FLV and MOV files from being hot-lnked. Simply add as many extensions needed (ext1|ext2|ext3…)

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?domain.com/ .*$ [NC]
RewriteRule .(flv|swf|mov)$ /feed/ [R=302,L]

Display .html pages instead of .php pages

This is used if you want to hide the fact you are using php. It could also help with SEO.

RewriteRule ^(.*).html$ $1.php [R=301,L]

Serve up a default image

Normally if you are linking to an image that no longer exists you usually get an error. With the code below you can specify a default image.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^imgDirectory/.*.jpg$ /imgDirectory/default.jpg [L]

Redirect to a temporary maintenance page

Use this if you are making updates and want the site down temporarily.

RedirectMatch 302 ^/ /comingBackSoon.html

Permanantely move a page

Sometimes you want to change a page name but it’s linked from hundreds of locations. The code below will redirect the request to the new page.

RewriteCond %{REQUEST_URI} ^/oldPage/index.html [NC]
RewriteRule /(.*) http://www.myDomain.com/newPage/index.html [R=301,L]

If you have any rules that you feel may be helpful to others please post a comment and we will add them to the list.

Read Also:

  1. using a host file to test a virtual host apache site configuration
  2. Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

Tags:

Leave a Reply

Spam protection by WP Captcha-Free