1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Setting Up 301 Redirects

Discussion in 'Server Configuration and Hosting' started by TheBigK, Oct 4, 2012.

  1. TheBigK

    TheBigK Well-Known Member

    As most of you can guess, I'm into the desperation mode to fix the 404 errors on my site. I've asked this questions at several places but XF seems to be the only place where I get reliable answers. I'd really appreciate help on setting up 301 redirects for the large number of tags that I deleted from my 'wordpress' blog.

    I want to setup a 301 redirect to the homepage only for the deleted tags from domain.com/tag/<deleted.tag> => domain.com. I want to keep the regular 404 pages though for other locations that are 'not found'.

    I'm not sure whether I should add it to .htaccess or setup a function in wordpress (if yes, what'd be the location of the file that I need to edit?)
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    TheBigK and Chris D like this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    Nice one Jake.

    I actually fixed this for BigK the other day.

    WordPress forwards you to a theme specific 404.php page when a page is not found.

    e.g. http://www.crazyengineers.com/jakeistheman

    I amended the 404.php file to include this:

    if (strstr($_SERVER['REQUEST_URI'], '/tag/'))
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ".get_bloginfo('url'));
    Therefore, now any 404 page containing /tag/ will be 301 redirected to the site home page, e.g. http://www.crazyengineers.com/tag/jakeistheman whereas any other 404 is dealt with normally.
    rhodes, TheBigK and Jake Bunce like this.
  4. TheBigK

    TheBigK Well-Known Member

    Thanks Jake & Chris. :)
  5. TheBigK

    TheBigK Well-Known Member

    Looks like my 404 errors aren't going away. I found out that after uninstalling the 'events' add-on (Xenatendo) - there are a large number of 404 pages for all the calendar entries. What would be the rule to divert all best way to turn domain.com/community/events/ to domain.com/community/ ? (301 redirects).

    Also, which htaccess file should I edit (the one under main domain or the one under /community/ ? )
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Add to .htaccess in /community directory:

    #	Mod_security can interfere with uploading of content such as attachments. If you
    #	cannot attach files, remove the "#" from the lines below.
    #<IfModule mod_security.c>
    #	SecFilterEngine Off
    #	SecFilterScanPOST Off
    ErrorDocument 401 default
    ErrorDocument 403 default
    ErrorDocument 404 default
    ErrorDocument 500 default
    <IfModule mod_rewrite.c>
    	RewriteEngine On
    	RewriteRule ^events/.*$ http://www.yoursite.com/community/? [R=301,L]
    	#	If you are having problems with the rewrite rules, remove the "#" from the
    	#	line that begins "RewriteBase" below. You will also have to change the path
    	#	of the rewrite to reflect the path to your XenForo installation.
    	#RewriteBase /xenforo
    	#	This line may be needed to enable WebDAV editing with PHP as a CGI.
    	#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    	RewriteCond %{REQUEST_FILENAME} -f [OR]
    	RewriteCond %{REQUEST_FILENAME} -l [OR]
    	RewriteCond %{REQUEST_FILENAME} -d
    	RewriteRule ^.*$ - [NC,L]
    	RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
    	RewriteRule ^.*$ index.php [NC,L]
    IMO this isn't a problem to begin with. 404 means the page no longer exists which is expected because you removed it. Not sure how that's a problem.
  7. TheBigK

    TheBigK Well-Known Member

    Thank you, Jake. My concern at the moment is to make sure that the Google Webmaster Tools shows < 2k 'Page Not Found' errors. Currently it's at 96k page not found errors which is down by 3k after I fixed the broken tags on my wordpress installation. I've arrived at a conclusion that the page not found errors are causing the site rankings to go down, despite google's post that said 404s don't affect page rankings.
  8. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    If a 404 lowers rank (which it shouldn't) then I'm not sure how a 301 would help. You are saving none of the original content in this case.
  9. TheBigK

    TheBigK Well-Known Member

    Well, I'm not sure if I'm doing the right thing. Google reported traffic going down just at the same time it reported large 404 errors. I've searched around and found a few people saying the same: large number of 404 errors causing SERPs to go down.

    Google takes lot of time to drop 404s from their index and I can't afford to lose our traffic for that long. Is there any other way to deal with the situation?
  10. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    I don't know. But you are redirecting people away from a page that no longer exists to a page that isn't what they wanted. I think the 404 is more useful to the end user.

    When you delete content you should expect to lose that traffic, no?
  11. hellreturn

    hellreturn Active Member

    I would also confirm that too many 404's is not good but I wouldn't suggest to add 301 for URL that doesn't exist. We had tried that before and outcome wasn't good. Google will think you have hundreds of link for similar content.. and end result wouldn't be too good. But then again this happened to my friend who was using vB 3.8.x series.

    Google will slowly delete those pages. It would take time but it wouldn't be to long. If you have extra resources on your machine increase your crawl rate to speed up the process.
  12. TheBigK

    TheBigK Well-Known Member

    I found out the issue - and it was with the 'Disqus' plugin we used on Wordpress. Several other webmasters have had the same issue and I've resolved most of them. Few URLs that I wish to redirect are really old ones and I'd really appreciate if you could comment whether the .htaccess rule would be written as follows -

    Redirect 301 /old-path-linked-by-another-website /correct-path-on-my-site
    Do I need to write 'ReadWriteEngineOn' before I write that (if it's already written at the top of the .htaccess file)?

    I've about 15 such distinct URLs that I need to their correct locations.
  13. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    No. Those Redirect commands don't require you to enable the RewriteEngine.
  14. TheBigK

    TheBigK Well-Known Member

    Thanks, is the syntax correct? Should I make a list of the URLs that need to be redirected viz -

    Redirect 301 /incorrect-url-linked /correct-url-on-site/
    Redirect 301 /another-url/ /correct-url/
    .... and so on?
  15. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Yes that is correct.
  16. rhodes

    rhodes Active Member

    I have a lot of 404 errors due to deleted users (e.g. /member/1234 ) and would like to do sth similar. If a link to a profile page causes an 404 error I would like to redirect (301) this profile page.

    Any ideas how I could do this with XF1.5. @Chris D ?
  17. Chris D

    Chris D XenForo Developer Staff Member

    I've somewhat changed my outlook on such things over the years.

    It's frankly not worth the effort. People have this impression that they couldn't possibly ever return a 404 because instantly Google will start penalising you. This is far from the case.

    Google Webmaster Tools and such notify you of 404s in an advisory sort of fashion merely to highlight any potential mistakes. For example, if all of a sudden ALL of your members pages started returning 404 then that'd be some sort of fault that needs resolving. However, if you have comparatively small number of URLs returning a 404, this is normal and basically expected in the grand scheme of things, and returning a 404 is a perfectly normal thing to do when content is not found.

    When Google hits a 404 and that status is maintained for some time, it will just trigger them to remove that entry from its index eventually so the "problem" actually solves itself automatically.

    Coupled with an automatically submitted Sitemap, this is more than sufficient and won't require you to take any further action.

    Clearly if we thought that returning a 301 redirect rather than a 404 was more beneficial, the software would already do that :)
    rhodes likes this.
  18. rhodes

    rhodes Active Member

Share This Page