homepage Welcome to WebmasterWorld Guest from 54.211.201.65
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
404 redirect issue
jo3y




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

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:

# CUSTOM 404 ERROR PAGE
ErrorDocument 404 /error404.htm
#
# REDIRECT SHTML AND HTML TO PHP
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?

 

jo3y




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

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?

g1smd




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

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
\.s?html? pattern.

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.

jo3y




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved