Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect entire directory of 404 pages (deleted directory)

         

Sgt_Kickaxe

12:31 am on Jun 8, 2010 (gmt 0)



I've been searching for advice on redirecting an entire directory of soon to be missing pages (deleted forum). All of the pages in folder /widgets-forum/ will soon return a 404 error and I'd like to redirect them all to a specific page www.example.com/archives/

It sounds like a simple proposition, and I'm sure it is, but all of the examples I've tried return www.example.com/archives/EXTRA STUFF HERE and I don't want any extra stuff, the pages don't exist.

I've read through the documentation here and it deals primarily with moving content, how would I make it work with 404 errors and a single end uri?

g1smd

7:27 am on Jun 8, 2010 (gmt 0)

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



Once the server has sent a 404 error for the requested URL it is too late to be redirecting the user to a different URL.

You've already sent a 404 status code instead of a 301 status code.

So, if you want to redirect a whole folder of URLs simply use a RewriteRule with the [R=301,L] flags to do this.

In the .htaccess file in the folder to be redirected:

RewriteRule .* http://www.example.com/new-URL [R=301,L]


Upon requesting any URL in that folder, the user will be immediately redirected to the new URL.

A 404 status means the pages have "gone away", and a 301 status says the user should "go over there".

You can send one or the other, not both.

Sgt_Kickaxe

3:34 am on Jun 9, 2010 (gmt 0)



Got it, and tried the example.

http://www.example.com/old-URL?1234 turns into http://www.example.com/new-URL?1234 with that example.

How do I stop it from attaching anything more than the exact 301 destination I specify? ie: no ?1234 being added.

it would be
RewriteRule .* http://www.example.com/new-URL? [R=301,L]

correct ?

jdMorgan

4:33 am on Jun 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As discussed in the concurrent Google Search thread, and in the Webmaster help page for "Soft 404 Errors," this "301 all missing pages to one page" is a very bad idea.

Better to serve a custom 404 or 410 error page for those URLs, and to make the "archive" page the ErrorDocument for the directory to which you rewrite them.

For example, in the "forum" directory .htaccess file:

RewriteCond %{QUERY_STRING} ^1234$
RewriteRule ^old-URL$ /archive/file-path-which-does-not-exist.hmtl [L]

and in /archive/.htaccess

ErrorDocument 404 /archive/archived-stuff-page.html


-or-

Forum directory .htaccess file:

RewriteCond %{QUERY_STRING} ^1234$
RewriteRule ^old-URL$ /archive/forum-removed.html [L]

and in /archive/.htaccess

RewriteRule ^forum-removed\.html$ - [G]
#
ErrorDocument 410 /archive/archived-stuff-page.html


Note that neither /archive/file-path-which-does-not-exist.hmtl nor /archive/forum-removed.html exist as real files.

In either the 404-Not Found or the 410-Gone implementation example, the archived-stuff-page.html should contain links to your home page, major categories, HTML site map, and site search facility. It can even look almost exactly like your home page or old "forum" page if you like. As long as a 404-Not Found or 410-Gone status is returned to the client, you won't have any trouble with search indexing or ranking.

The above examples may actually be more complicated that what you really need. If your old forum URLs all indicated a subdirectory, then you wont need the rewrite; Just put the errordocument directive and the custom error page in that previously-existing subdirectory.

Jim