Welcome to WebmasterWorld Guest from 54.167.174.11

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Try another directory before giving a 404. How to?

   
2:09 am on Apr 23, 2011 (gmt 0)

5+ Year Member



I'd like to use .htaccess and some rewrite rules to try looking for broken links in another directory before throwing out a 404. Anyone know of an easy way to go about this?

I'm moving a bunch of stuff, and just in case I've missed something I'd like to have a fail-safe of some sort.
5:40 pm on Apr 25, 2011 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



That depends on the semantic relationship between the correct and incorrect paths... and which parts of those paths are variable and which are unchanging.

A simple example that tries top-level directory "b" after the request fails for top-level directory "a" would be:

# If requested URL-path does not resolve to existing file in "/a/" path
RewriteCond %{REQUEST_FILENAME} !-f
# but does resolve to an existing file in the "/b/" path
RewriteCond %{DOCUMENT_ROOT}/b/$1 -f
# then externally redirect the client to a URL referring to the "/b/" path
RewriteRule ^a/(.*)$ http://www.example.com/b/$1 [R=301,L]

Note that this code will do one or two filesystem accesses for every request to the "/a/" URL-path. As such, it may slow down your server if you have a lot of traffic and there is either a lot of memory<->disk swapping or if your disk is already busy for other reasons.

Jim
8:49 pm on Apr 25, 2011 (gmt 0)

5+ Year Member



Ah! I see. That makes sense. Thank you so much, it totally works. This will help until we can go thru the site and check/fix any outdated links.

With your example I was able to look up the definitions of the switches and server variables. It was just hard to do that without a starting point (even with the apache manual):

[httpd.apache.org...]

Thanks again!