Rewrite URLs on Apache with mod_rewrite

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.

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

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 – rpardz.com/about/ rather than rpardz.com/about/index.php.

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.