Redirecting Site Pages With .htaccess

Using .htaccess to help manage your website changes

If you change a file name of one or more of your web pages and a visitor tries to access the old page saved in their browser they will get a 404 page (not found) response from your hosting server. So what's the best way of ensuring all your visitors end up on the right page?

Many people, including web designers, think that page redirects are the solution i.e. the "meta http-equiv="refresh" command, but search engines are not keen on a site that has these automatic redirects, as this technique is often used by unscrupulous spammers and other low life who redirect people to questionable sites. This option should therefore be avoided if you have more than one page to redirect.

You could use PHP to achieve a few basic page redirections:

<?php header("Refresh: 4; url=http://www.yourdomain.co.uk/page.html");

Of course your page will have to have the extension .php to work.

But if you have quite a few pages to redirect its best to use a 301 redirect and this works well on an apache server model running under a Windows or a Unix operating system.

In the root of your site i.e. www.yourdomain.co.uk/ create a file named .htaccess. The best way to create this file is to open up notepad and save the document as a .htaccess file. Make sure you change the 'save as type' to all files. This will prevent the .htaccess file being saved as a text document.

Open your .htaccess file using notepad and type the following redirect request:

Redirect 301 / directory/old file.html http://www.yourdomain.co.uk/directory/filenew.html

The first part of this command (/ directory/old file.html) is the location of the old directory and/or file which has to be moved.

The second part (http://www.yourdomain.co.uk/directory/filenew.html) is where the directory and/or file has been moved to.

Now all you need to do is upload the .htaccess file to your sites root e.g. www.yourdomain.co.uk/.htaccess.

You can use the same technique to redirect an old domain name to a new one e.g. www.yourdomain.co.uk to www.yourdomain.com and even change all your web page extensions from htm to html or from html to php.

Changing Web Page Extensions

To change web page extensions from htm to html we need to introduce the request RedirectMatch which makes use of regular expressions to achieve this task.

In your .htaccess file add:

RedirectMatch 301 (.*)\.htm$ http://www.yourdomain.co.uk$1.html

The result of this is that when a site visitor types www.yourdomain.co.uk/page.htm into a browser the HTTP request will find and open www.yourdomain.co.uk/page.html. And the same result will be achieved for every page of your website.

Redirecting an old Domain to a New One

If you simply want to redirect all requests directed to an old domain on to a new one use the following request:

RewriteRule /.* https://www.yournewdomain.com/ [R=301,L].

No matter what file or folder is requested they will all go to the new domain. So requests to https://www.yourdomain.co.uk will be redirected to https://www.yournewdomain.com/

Again upload your .htaccess file to to your web servers root and you will find that 301 redirects are seamless - you would hardly know that a redirection had taken place.

Using your own design for your 404 Page

If you want to prepare your own 404 page (page not found) to fit in with your websites overall design you again need to create or modify the .htaccess file in the root directory of your website.

ErrorDocument 404 /404error.html

This tells the server to redirect any 404 errors to the page error.html. Make sure that you actually call your newly designed page 404error.html and place it in the root directory of your server. Full a full guide about how to do this see our web design article - create a custom 404 File Not Found Page.

Other common error codes

Error 403

This is most often encountered when the index.html of a folder is missing or the owner of the site has turned off permissions for either that page or its parent directory. If you get this error message and really want to see the page, you could try and contact the owner of the site and tell them what URL you were trying to access when you got the 403 error.
Permissions get changed as much by accident as on purpose and the web designer might appreciate knowing.

Error 500
This means that there was a cgi script running that didn't work correctly, you can't fix it, and you can't move past it so again the best advice is to contact the owner of the site.

You can also prepare your own custom 403 and 500 error files if you wish. The principal is just the same:

ErrorDocument 403 /403error.html

ErrorDocument 500 /500error.html

Here's a sample .htaccess file to show the important spacing between the commands.

QBS PC Help use a few of these 301 requests (and 404 requests) as the current location of pages for the latest site are different to the old one and some pages don't even exist any more so have to be redirected. We host our site with Fast2Host on an apache server using the linux operating system.

If you would like to find out more about server error codes see our article - Server Error Codes (HTTP status codes).

Read more articles about PC repairs, Web design & SEO...