Forum Moderators: phranque

Message Too Old, No Replies

return 301 instead of 404

404 301 redirect

         

techtheatre

5:40 am on Apr 13, 2009 (gmt 0)

10+ Year Member



So, I have a somewhat tricky question (at least I think it is tricky). I am somewhat familiar with using a .htaccess file to 301 redirect users to a page of my website when I have changed a name or path or whatever. I know this is good for SEO (page rank) and simply for being helpful to my visitors. I have a website now where we post a PDF newsletter every week or so, and then after about 6 months delete the file (simply to save clutter on the server, as the old ones are not of any useful archival value). I like that currently Google indexes my PDF files and would rather not robots.txt them out of the index. Unfortunately, when i go in and delete old PDF files, Google then notices them missing and the 404 issue arises. Is there a way to write some magic .htaccess command (or other technique) that will 301 redirect deleted PDF requests to a specific page (let's call it [mysite.com...] so that even though THAT newsletter doesn't exist anymore, people may still find another one that is of interest (and any Google points will transfer as well). I don't want to do this with a custom 404 page because, while my users would be fine (i could display a list of current newsletters on the custom 404), Google would continue to punish my pagerank for constantly having broken pages. Thanks!

g1smd

7:58 am on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, set up a rewrite that rewrites the URL request to a script if the file is missing, and the script will then have an array or database table lookup to get the real URL to be redirected to. The script will send the 301 header and the location header.

The filesystem check for 'file exists' is expensive in processing time and efficiency, so it might be better to go one step further and rewrite all requests for PDF to the script and let the script send the file if one exists and return the 301 for files that no longer exist.

Whatever you do, it is very important that the script does one further thing. If the URL being asked for has never existed, it must return a 404 for that request. That is, it should only return a 301 for URLs that did exist and which no longer exist.

techtheatre

8:11 am on Apr 13, 2009 (gmt 0)

10+ Year Member



I followed your second comment about having a script serve up the PDF, but did not understand what you mean/how to implement the first solution:

"set up a rewrite that rewrites the URL request to a script if the file is missing"

techtheatre

12:46 am on Apr 16, 2009 (gmt 0)

10+ Year Member



hello? anyone? I understood the second comment but not the first. Can anyone help? Thanks!