A simple look at using mod_rewrite on Apache servers
Is your website accessible by typing www.yourdomain.com as well as yourdomain.com, well using mod_rewrite on Apache servers you will be able to control how your URL structure is displayed whichever way the user types in your address. Controlling the URL structure is in fact more important than some people realise.
Personal Example
I have used mod_rewrite on this website so that if the user types www.brightscape.net, brightscape.net, or blog.brightscape.net (the old blog URL), they see a single uniform URL structure. I chose not to display ‘www.’ for my domain. This is personal preference, however I feel the shorter the URL the better. Unnecessary clutter can make your domain name less memorable.
With mod_rewrite on Apache, you still receive all your visitors no matter which of the above URLs they type into the address bar, however, they will see the URL change to your preset structure. Below is an example of how I used mod_rewrite on my own website. I will display examples of how this can be acheived on your own website if it is running on Apache.
Step by step guide to using some of the simple mod_rewrite functions
1. Create an .htaccess file
You first need to create an ‘.htaccess’ file that will be placed in your website root folder. To do this open Notepad or another text editor. You simply need to save the empty text file as .htaccess. There should be no other file extention such as .txt, so to do this in Notepad click Save As > Save as type > type in .htaccess > Save. Leave the file open so that we can now add our mod_rewrite code.
2. Add the mod_rewrite code to .htaccess
If you want to display your URLs with www. then use the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
Copy the above code into your .htaccess file, remembering to change the URL to match your own URL. Save the file and upload it to your root folder. Now test to check that it works correctly. Do this by typing yourdomain.com into the address bar. The URL should change to www.yourdomain.com and load the page. This usually happens in a split second.
If you prefer to display your URL without www. then use the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^yourdomain.com$ [NC]
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]
As with the previous example, copy the above code into your .htaccess file, remembering to change the URL to match your own URL. Save the file and upload it to your root folder. Test to check that it works correctly. Do this by typing www.yourdomain.com into the address bar. The URL should change to yourdomain.com and load the page.
Additional Options
You can take the above examples further. If you set out your website using a folder structure with index.htm/.html/.php for the file names, then you can make sure that only the folder is displayed like so - brightscape.net/about rather than brightscape.net/about/index.php. (Please note this is an example only, I have not used this on brightscape.net).
To do this you can add the following code to either of the above examples below the coding you have already used.
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|htm|html)\ HTTP
RewriteRule ^(.*)index\.(php|htm|html)$ /$1 [R=301,L]
This will make sure that the folder structure displays the page properly, but ‘index.php’ will not appear in the address bar. You can add additional file extensions by separating them with the pipe symbol | as shown in the above code.




3 Responses
Jason
September 4th, 2007 at 9:13 am
1Hi Rob,
Previously I’ve done all my redirections in php, through include files. This has allowed me to rewrite links dynamically dependent on the location of the hosting.
Re-writing with .htaccess is something that I have meant to implement on my public hosting for some time but never got round to.
Just thought I’d let you know that I have tested your code on my testbed domain.
If you go to http://www.x888.co.uk/spoon/index.htm you should get redirected to x888.co.uk/spoon
Cheers!
Rob
September 4th, 2007 at 9:50 am
2Thanks Jason,
I’m glad it worked for you.
I had been hunting around for some solutions for a while, but finally found that solution that worked pretty well for me. I hope it proves useful to others as well.
I’m definitely no regex expert, so I have to keep it simple
Jimmy
September 14th, 2007 at 11:50 am
3Yes that it just what I was going to say..
Are you familiar with the term ‘geek chic’?
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Popular Posts
Links
Meta