Help setting up redirect for "not found" pages

granger99

New Member
Messages
33
Reaction score
0
Points
0
I would like to have one redirect for all "not found" pages on my site. Ideally, any such pages would be redirected to the home page. Is this possible without knowing which pages they are? One way is in the .htaccess file, like this perhaps?

RewriteEngine On

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

RewriteRule \/.+ / [L,R]



Or can I do it via cPanel somehow?

Thanks for your help!
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Maybe try...


Code:
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule .* /index.html  [L]

if the homepage is index.html (adjust to your case)
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You could also set a script as the error handler using ErrorDocument, and have that script redirect. You could even set the index page as the error document. However, I recommend against redirecting without some sort of not-found notice. It breaks people's expectations and doesn't play well with old links and bookmarks. Better to have a custom error page that includes links to the main page and to pages with similar URLs (which can be tricky to implement).
 

granger99

New Member
Messages
33
Reaction score
0
Points
0
Thanks for the replies - basically I have heard that the search engines don't like 404 errors, and so it's best to change all not found pages to 301 or 302 redirects.

My purpose is less for the reader and more for the purpose of keeping the search engines happy by having as few error pages as possible.

With that in mind, would you still make the same recommendations?

Thank you again for trying to help me.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Thanks for the replies - basically I have heard that the search engines don't like 404 errors, and so it's best to change all not found pages to 301 or 302 redirects.
What, exactly, did the source(s) say the problem was? A 404 means no resource exists at the given URL. If the URL used to be valid but isn't any longer, a 301 is appropriate (but ask yourself: why did the URL change? As the W3C says, "Cool URIs don't change"). If a URL doesn't name something on the site, a 404 is the most appropriate response. The only inappropriate response you commonly see is returning a 200 for an error page. If a non-existent URL results in a redirect to the home (or other prominent) page, the search engine might mark you down for duplicate content.

A search engine will only come across a 404 when following a dead link or a typo. In both cases, your log should reveal the referrer URL that contains the bad link, letting you contact the site to correct it.

With that in mind, would you still make the same recommendations?
Yes. Keep the 404s with well-designed error pages. There are countless pages that discuss this topic. Let Google be your guide.
 

granger99

New Member
Messages
33
Reaction score
0
Points
0
The reason my URL's have changed is because I changed them! Specifically, I changed the command in wordpress to /%postname%/ which made all of the previous URL's invalid. Therefore, for any pages Google has already indexed (my blog is still quite young), I want to make sure a 404 error doesn't occur.

Is there a silver bullet here? For example, change a line or two in the htaccess and all of these old URL's end up on my home page?
 

slacker3

New Member
Messages
146
Reaction score
6
Points
0
The reason my URL's have changed is because I changed them! Specifically, I changed the command in wordpress to /%postname%/ which made all of the previous URL's invalid. Therefore, for any pages Google has already indexed (my blog is still quite young), I want to make sure a 404 error doesn't occur.

Is there a silver bullet here? For example, change a line or two in the htaccess and all of these old URL's end up on my home page?


as google says:
Google updates its entire index regularly. When we crawl the web, we automatically find new pages, remove outdated links, and reflect updates to existing pages, keeping the Google index fresh and as up-to-date as possible.
If outdated pages from your site appear in the search results, ensure that the pages return a status of either 404 (not found) or 410 (gone) in the header. These status codes tell Googlebot that the requested URL isn't valid. Some servers are misconfigured to return a status of 200 (Successful) for pages that don't exist, which tells Googlebot that the requested URLs are valid and should be indexed. If a page returns a true 404 error via the HTTP headers, anyone can remove it from the Google index using the URL removal tool. Outdated pages that don't return true 404 errors usually fall out of our index naturally when other pages stop linking to them.
http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=156412

http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=92865

http://labnol.blogspot.com/2005/09/clear-google-web-cache-delete-404.html

EDIT:
my suggestion would be to register at google webmaster tools to make an url removal request (shouldn't be necessary, tough) and/or create an custom 404 page and redirect with javascript to the main page (or some other desired page..)

google won't follow javascript redirects (as far as i know)
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The reason my URL's have changed is because I changed them!
But why did you need to change them?

In this case, you should redirect the old URLs to the new ones so they can be found, rather than redirecting them all to the home page. You can use the rewrite engine or RedirectMatch, which is tailored for redirects.
 
Last edited:

granger99

New Member
Messages
33
Reaction score
0
Points
0
Thank you very much, I will spend some time digesting your suggestions and then get back to you. I really appreciate your help!

Just one more question for now - I realize that you don't approve of me sending all redirects to my home page, but if I truly want to do it that way, is it possible? What would be the code in my htaccess necessary to achieve that?
 
Last edited:

vishal

-::-X10 Guru-::-
Community Support
Messages
5,255
Reaction score
192
Points
63
I tried this but was not successfull.
 

granger99

New Member
Messages
33
Reaction score
0
Points
0
Here are some of the "Not Found" URLs. Can I set up permanent redirects in cPanel for them?
-------------------------------------------------------------------------
Required but not found URLs (HTTP code 404)

URL (5)

/mobi/
/mobile/
/iphone/
/m/
/apple-touch-icon.png
 

tillabong

New Member
Messages
60
Reaction score
0
Points
0
Maybe try...


Code:
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule .* /index.html  [L]

if the homepage is index.html (adjust to your case)

sorry but im a bit confused here. if my website is www.mysite.com and people just enter a random url like www.mysite.com/sdfdsf which does not exist, they will get x10hosting error 404. so if i change this will the problem be solved? as in the user will be redirected to the homepage? thanks.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Just one more question for now - I realize that you don't approve of me sending all redirects to my home page, but if I truly want to do it that way, is it possible? What would be the code in my htaccess necessary to achieve that?
As noted before, you can either use ErrorDocument and have your error document redirect to wherever, or use RedirectMatch or RewriteRule (with the R=301 flag) to match specific outdated URLs. To match alternates in a regular expression, use a vertical bar: "mob(ile)?|iphone|m". Are there no longer pages corresponding to the URLs you list? If there are, you should redirect the old URLs to the new ones.


Code:
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule .* /index.html  [L]
sorry but im a bit confused here. if my website is www.mysite.com and people just enter a random url like www.mysite.com/sdfdsf which does not exist, they will get x10hosting error 404. so if i change this will the problem be solved? as in the user will be redirected to the homepage? thanks.
What problem? If a user enters a random path in a URL that doesn't name a resource, a "Not Found" response is entirely appropriate: the user asked for something by a non-existent named. If you called an office and asked to talk to a Arthur Putey, and Arthur didn't work there, you don't get transferred to the front desk, you get told there is no Arthur Putey. Think how confusing it would be if you asked for Arthur, the phone rang, and you end up talking to someone else. Similarly, the user should get a "Not Found" page with links to the main page and other major pages (read up on other "Not Found" error document design points).

What the code does is when a URL doesn't resolve to a directory or file, internally redirect to /index.html. This can hurt your search engine rankings as different URLs will have duplicate content; adding an [R=301] flag will fix this issue. You also want to make the RewriteRule the last one in .htaccess. Personally, I find it counterproductive.
 

tillabong

New Member
Messages
60
Reaction score
0
Points
0
ok i get it. thanks alot. actually i was thinking that i didnt want users to know what web host im using so if they enter a random url that doesnt fit the x10hosting error wont show up and it will direct them to my homepage instead.
 
Top