homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Try another directory before giving a 404. How to?

5+ Year Member

Msg#: 4302800 posted 2:09 am on Apr 23, 2011 (gmt 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.



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

Msg#: 4302800 posted 5:40 pm on Apr 25, 2011 (gmt 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.



5+ Year Member

Msg#: 4302800 posted 8:49 pm on Apr 25, 2011 (gmt 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!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved