homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Rewrite virtual directories to physical ones...
...while redirecting away from physical ones

 8:07 pm on May 15, 2003 (gmt 0)

Currently I've got two rules to map directories from their real container folder ("/catalog/") up to the root:

RewriteRule ^(cat¦dog¦cow)/(.*)$/catalog/$1/$2[L]
RewriteRule ^(cat¦dog¦cow)$/$1/[R]

I've tried putting

Redirect /catalog [test.com...]

before and after the rewrite rules, but I get stuck in a loop.

Anyone know the correct way to do this?



 8:41 pm on May 15, 2003 (gmt 0)

Doh! Forgot to mention the point is to keep people and bots out of the physical directory "/catalog/" and in the root ones [test.com...] [test.com...] etc.


 8:58 pm on May 15, 2003 (gmt 0)


Check out this thread for self-help: Introduction to mod_rewrite [webmasterworld.com]

If this doesn't help you, please give a few examples of the requested URL and what URL you want that request redirected to. Please include everything, including "http://mydomain.com" in both, just to be clear.

Also, do you use query strings?

I suspect your solution is an easy one, but as always, defining the problem thoroughly is the hard part.



 9:48 pm on May 15, 2003 (gmt 0)

Current behavior:

site.com/dog rewrites from actual file site.com/catalog/dog
site.com/cat rewrites from actual file site.com/catalog/cat
site.com/cow rewrites from actual file site.com/catalog/cow

Extra desired behavior:

site.com/catalog/[anything] redirect to site.com/


 10:47 pm on May 15, 2003 (gmt 0)


OK, as I understand it, you wish to accomplish three things:

  1. Redirect external requests for [site.com...] to [site.com...]
  2. Redirect external requests for [site.com...] to htpp://site.com/cat/ (Add a trailing slash, basically)
  3. Serve files from the catalog/cat subdirectory in response to external requests for the [site.com...] files.

If so, this should work. The first two are external 301 redirects, and the last one is an internal redirect. That is important, and the order of the last two rules is also important to prevent a loop.

RewriteRule ^catalog http://www.site.com/ [R=301,L]
RewriteRule ^(cat¦dog¦cow)$ http://www.site.com/$1/ [R=301,L]
RewriteRule ^(cat¦dog¦cow)/(.*)$ /catalog/$1/$2 [L]

Remember to replace the broken vertical pipe "¦" characters with the solid ones from your keyboard.



 2:47 pm on May 16, 2003 (gmt 0)

Thanks for the suggestion Jim.

The infinite loop problem is fixed, but now all requests for /cat, /dog, /cow or /catalog* are getting redirected to the root.

I swear I've seen to weird stuff like this before, like applying redirect rules to server side include paths.


 3:19 pm on May 16, 2003 (gmt 0)


> /catalog* are getting redirected to the root.

This redirect for /catalog is happening because of requirement number one, implemented in RewriteRule number 1. If the requirement is wrong, the Rule will be wrong, and it sounds like it is.

You can redirect anything that starts with catalog - including catalog followed by nothing, or anything that starts with catalog followed by a slash and anything else (including blank), or you can require that catalog/ be followed by another subdirectory and a slash - whatever you want. But the regular expressions used in mod_rewrite require that you be absolutely specific as to what you want rewritten and what you don't.

The redirects of cat¦dog¦cow may be taking place for the same reason - that the requirements are incorrect, so the Rule is incorrect. Or, they can be taking place because some down-stream .htaccess or script is doing another external (i.e. 301 or 302) redirect; In which case, the whole .htaccess file gets processed again. Take a look at your set-up and see if that might be the case. This could also be some weirdness in the server set-up (The SSI-include redirect thing sounds fishy to me).



 4:50 pm on May 22, 2003 (gmt 0)

Simplified example:

This rule below sends the file "real.html" in response to requests for "virtual.html".

RewriteRule ^virtual.html$ real.html [L]

Question is:
How do I redirect requests for "real.html" to "virtual"? All the .htaccess attempts I've tried result in loops.

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