Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- Redirect 404 as permanent, with www to non www


jd01 - 2:13 pm on Oct 3, 2009 (gmt 0)


I'm totally confused by some of your statements, so let me ask some questions... Maybe I'm totally missing or misunderstanding something?

I am pretty sure it is not as simple as using the error docuement, as that will not return a permanent 301, but a 200 ok.

1.) Why would an error doc return a 200 ok, when it's by definition an error document and is served with the appropriate error code, not a 200? A properly functioning 404 error document, though it parses and displays does not return a 200. It returns a 404.

2.) Why would the requested URL not be redirected from www.example.com to example.com prior to any 404 error being served when the .htaccess file, including all rewrite rules must be processed prior to any page being served, or the .htaccess file would do absolutely no good?

The .htaccess file should be processed on any request, prior to any requested location being served. IOW: Every 'external' request is run through the httpd.conf, then .htaccess before the server can even know if the resource exists, so if your server is functioning correctly, then it should absolutely redirect to example.com prior to serving a 404 error.

That needs to go to example.com/index.html but as a 301 permanent, with no www. I basically want to have all old url's be valid and working, when they resolve normally, those that do not, I want a 301 permanent.

3.) Why would you want to serve a 301 and redirect everything to the homepage?

(AFAIK it is Not recommended to do this. If the resource is not found it should serve a 404 (Not Found), and if it has been removed it should technically serve a 410 (Gone). If you are going to redirect a resource which has been removed or you know cannot be found it should be redirected to the new location of the resource, or the location of a similar resource on the same topic. Again AFAIK as far as search engine rankings go it is Not recommended to redirect every 'not found' request to the home page, or any other page for that matter. It is much wiser to serve a custom 404 or 410 error page with a mini-sitemap, sitemap or the 'most used and visited links'. It could even be done dynamically with a .php paged served as your 404 error page and show different links based on the REQUEST_URI.)

NOTE:
Your rule Does Not serve a 301 redirect. It serves a 302, or 'undefined' redirect, because the R is not followed by =301, which means it is undefined, or a 302.

Also, if you are going to use a 'catch all' on the left side of the rule, the / on the left side of the rule is unnecessary, simply remove the start anchor, and if the ruleset is in the httpd.conf, remove the / preceding $1 on the right side of the rule, if the ruleset is in the .htaccess, then leave the / where it is. NOTE: If the ruleset is in the .htaccess it won't match anything anyway, because the / is not present on the left side of the rule.

The two different versions follow.

# .htaccess Version

# Rewriting rules
RewriteEngine on

# Redirect if www to non-www, carry url arguments (case-insensitive)
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

##### ### #####

# httpd.conf Version

# Rewriting rules
RewriteEngine on

# Redirect if www to non-www, carry url arguments (case-insensitive)
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com$1 [R=301,L]

Sorry if I didn't really answer your question, but I hope this helps a little bit and gives you some ideas.

If I set the 404 error doc to a local path like /index.html I lose a lot of my images and css files loading.

What do you mean by lose? Where do they go? The page served should be exactly the same as what you see on the screen when you access the error page URL directly... Only the code served to the browser changes, so I'm not sure how you are losing images or files if they are present when you visit the page.


Thread source:: http://www.webmasterworld.com/apache/4000247.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com