htaccess: Stopping “page not found” errors

Stop Page not found errors when you restructure or move your site

Its a problem I come across often. Someone does a complete site redesign and all the filenames and paths have changed. They are really pleased with their new site. It looks good and works well. But what they don’t realise is their users aren’t so happy. Why? Well perhaps they have bookmarked one of your old pages. Or perhaps they type a search phrase into a search engine and it returns a link to your old pages. In both cases they will get “Page not found”.

301 Redirect

The solution to these problems is the “301 Redirect” and isn’t hard to do if your web hosting uses Apache which is a common situation. Unlike some other solutions to this problem, this method is also search engine friendly.

The method I will be explaining is how to redirect using .htaccess.

What is .htaccess

Its just a simple text file with an unusual name. Most file names you will be used to are like this: name.txt
There is a part before the dot and a part after which is known as the file extension.
With .htaccess there is nothing before the dot.

Making .htaccess

You can create .htaccess the same way you create any text file. All you need to be careful of is that after you have created it you need to check it REALLY is named .htaccess Some file editors will try and “help out” by appending .txt to the file name for example

I will explain some of the most common types of redirect you will want to do, but all of them will use the following as a basic .htaccess file so to start with put the following in your .htaccess file


<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

</IfModule>
This just checks that your webserver is configured correctly to allow .htaccess to work, switches the redirect feature on and tells it to work from the top level of your website. Add the commands given later in this tutorial after the RewriteBase / command.

I will now give some examples of typical situations in which you would use .htaccess. Choose the one that best solves your problem and add the commands shown to your .htaccess file

Simple Redirect to home page

The most basic redirect is for the situation where you don’t really have a specific page in mind. You just want them to go to your home page. Use this as a minimum, it will stop people getting page not found and is a useful stopgap solution while you wait for the search engines to catch up with your new website structure.

For example you may have previously had a load of webpages in a folder called htmlpages and the folder isn’t there anymore. Add the following and any requests for pages like http://site.com/htmlpages/index.php will be redirected to http://site.com/

RewriteRule ^htmlpages/(.*) http://site.com/ [R=301,L]

Just to explain what this means…
The ^ indicates start of line
(.*) means any combination of characters
so this rewrite rule will apply to any url that starts with htmlpages/ and is followed by absolutely anything
The [R=301,L] says “Do a 301 redirect” and “this is the last rule”.

So for your site just put whatever you want to “redirect from” after the ^ and replace “http://site.com/” with where you want to redirect to.

i.e.
RewriteRule ^<old page> http://newsite.com [R=301,L]

<old page> needs to be specified as a regular expression. If you don’t know what a regular expression is just type regex into a search engine to find many sites that will explain in more detail.

Redirect because of a renamed directory

In this case use the following

RewriteRule ^old_directory/(.*) new_directory/$1 [R=301,L]

This is very similar to before but in this case instead of dumping everything to the home page we are now going to be more precise.

The key here is the use of the $1 at the end of the new_directory. This tells it to replace the $1 with whatever was in the first regular expression group. In our case the (.*) so a rule like this

RewriteRule ^htmlpages/(.*) http://www.merseysidetoday.co.uk/code/$1 [R=301,L]

would cause a url of http://www.merseysidetoday.co.uk/htmlpages/index.php to be rewritten to http://www.merseysidetoday.co.uk/code/index.php

Redirect because you have renamed .html files to .php

You may have a website with static html pages that you want to change to have dynamic content. This will mean a rename to have the .php extension. To stop the dreaded “page not found” you can redirect all .html to be .php

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

this means redirect <anything>.html to <anything>.php

Redirecting web pages with query strings

If your url has a query string in it you can’t match it using just a RewriteRule command. You must use RewriteCond.
The RewriteCond command goes on the line before RewriteRule as shown below.

I recently wanted to redirect wordpress blog pages to corresponding VBulletin blog articles. Not a very common situation I suppose but it provides a good example of how to redirect pages that contain query strings. e.g. http://www.designertuts.com/blog/?p=8 (p=8 is the query string)

To redirect http://designertuts.com/blog/?p=8 to http://designertuts.com/forum/blog.php?b=15 use the following command (due to its length the RewriteRule has split onto 2 lines but you should type it on a single line)

RewriteCond %{QUERY_STRING} ^p=8$
RewriteRule ^blog/$ http://designertuts.com/forum/blog.php?b=15 [R=301,L]

The RewriteCond line says apply the following RewriteRule if the url ends with the query string ?p=8
The RewriteRule says rewrite urls ending with blog/ (and we know from the RewriteCond that they will also have ?p=8) to http://designertuts.com/forum/blog.php?b=15

Upload .htaccess

Now we need to upload it to our webserver. Before you do upload it make sure there isn’t an existing htaccess file there already. If there is you will need to look at what’s in it and see if it’s possible to merge together the existing commands with what you are trying to do. If all is well just use your favourite ftp program as usual making sure it is uploaded in ascii not binary mode.
You can place it wherever you want. Its effects will be felt in the folder it is placed in and in any subfolders.

Notes

Use of .htaccess is only appropriate when you DON’T have control over your webserver’s configuration. If you are the administrator then it is better to put commands in your httpd.conf file as this will lead to faster processing of the commands.

Make sure your hosting allows htaccess files. Ones that do may still restrict what you are allowed to do with them.

Watch out for .htaccess files in other directories as they may conflict with each other.

A good summary can be found here
http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/
Any problems leave me a comment.

Mike

One Response to “htaccess: Stopping “page not found” errors”

  • Scritube says:

    i have a website and i have a problem with phpsessionid propagation on my urls.

    Now i solve that problem but google indexes some pages , withc now i’ve deleted.

    I have a lot of page cannot be displayed.

    Can i use .htaccess to solve that problem with your examples?

Leave a Reply

New Discussion Forum

We have teamed up with CrypticGFX.com who are providing a place to discuss our tutorials.

You can still post blog comments as before but if you want a more in-depth discussion have a look at Cryptic GFX.