Welcome to WebmasterWorld Guest from

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)

New User

10+ Year Member

joined:June 21, 2005
posts: 26
votes: 0

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)

Senior Member

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

joined:Mar 31, 2002
votes: 0

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.

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

New User

10+ Year Member

joined:June 21, 2005
posts: 26
votes: 0

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):


Thanks again!

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members