Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

404 redirect issue



8:59 pm on Apr 26, 2011 (gmt 0)

5+ Year Member

Hi, my 404 redirect error is not working 100% and I can't figure out why. Here is what I did;

1.) Converted an old site from shtml to php. So, I am using .htaccess to change the extensions(shtml and html) to new extensions (php) so outside links from external sources need not change.

2.) I also created a custom 404 page for 404 redirects.

Here is the code:

ErrorDocument 404 /error404.htm
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.shtml$ http://web.com/$1.php [R=302,NC]
RewriteRule ^(.*)\.html$ http://web.com/$1.php [R=302,NC]

Now, let's say I type in index.shtml. The extension is changed to .php, and is redirected to the index.php as a permanent move(302). Same with the .html extension. If I type in index.html, the site is redirected to index.php as a permanent move. So that part works great.

Now if I type in asdfagw.htm(an extension not included in the .htaccess), the custom 404 page error works great.

Here is the problem ... when I use an extension that I change via the .htaccess the custom error page does not work. So, if I type asdfwfvwrt.html, I get the standard 404 error (not my custom). If I type in asdfas.shtml I get the standard 404 error (again not mine just the default). But again if I type in asdfwvwv.htm, it works fine.

Anyone know why the changed extensions aren't pulling up my custom error page?


9:35 pm on Apr 26, 2011 (gmt 0)

5+ Year Member

Ok some added information here, if I type in asdf2sdfs.php, the custom 404 page is not displayed.

So it turns out that any invalid page with the extensions (htm, html, shtml) etc. return the proper 404 custom page like it should.

However if I do an invalid page with .php extension I get the non-custom 404 standard error page. The .htaccess rewrite is changing the .shtml and .html extensions to .php, so the root of the problem is that .php extensions will not return a custom 404 page.

Anyone know why this would be the case?


10:34 pm on Apr 26, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

You should not link to any named index file, nor redirect to a named index file. Instead you should redirect to remove those elements. You will need the following code for that:

DirectoryIndex index.php

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(s?html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(s?html?|php)$ http://www.example.com/$1 [R=301,L]

The 302 is not a permanent redirect. The 301 is the permanent one.

You need the [L] flag on every rule.

Using (.*) is the worst possible pattern.

You don't need two rules here. They can be combined into one by using the

This code:

RewriteRule ^(.*)\.shtml$ http://web.com/$1.php [R=302,NC]
RewriteRule ^(.*)\.html$ http://web.com/$1.php [R=302,NC]

simplifies to:

RewriteRule ^(([^/]+/)*[^/.]+)\.s?html?$ http://example.com/$1.php [R=301,L]

You'll need to follow those with a standard non-www to www canonical redirect.

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

So, I am using .htaccess to change the extensions(shtml and html) to new extensions (php) so outside links from external sources need not change.

Those other links should be changed to point to the new URLs. However, the fact that your old URLs now redirect should be a big enough hint to other "webmasters with a clue" that they need to take action.

If you had wanted to retain the old URLs on the new site, and hide the fact the site uses PHP, then you should have used an internal rewrite instead of an external redirect.


11:54 pm on Apr 26, 2011 (gmt 0)

5+ Year Member

Thank you this is VERY helpful. Regex and mod_rewrite give me a bit of trouble.

Featured Threads

Hot Threads This Week

Hot Threads This Month