Forum Moderators: phranque

Message Too Old, No Replies

Internal rewrite root relative links to subdirectory

Mod_rewrite Apache 1.3.41

         

Roan

1:54 pm on Sep 12, 2009 (gmt 0)

10+ Year Member



Hi, hope you'll spare the time. I've looked through the tech docs and ended up more confused, and I've hesitated to post here because I suspect I'll come across as having not really tried or researched which will be sad because it's incorrect.

I'm attempting to internally rewrite (root relative request):
GET /images/sect1/thisimage.jpg - to -
/siteB_v10/images/sect1/thisimage.jpg

What I'm stumped with is how I capture the subdirectory variable (because RewriteCond just tests, doesn't capture?) and pass it to the RewriteRule when it's not in the GET request being made and I haven't been able to connect with/see this in the myriad of examples around. I believe the information is in REQUEST_FILENAME, is that correct? Is this even possible with mod_rewrite or should I be looking at another way - say.. just redirect everything to a php script that will sort it out?

Thanks for stopping by.

jdMorgan

2:27 pm on Sep 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteCond can be used to capture, just like RewriteRule. Strings matching parenthesized sub-patterns in RewriteRule are back-referenced using local variables $1 through $9. Strings matching parenthesized sub-patterns in the last-matched RewriteCond are back-referenced using local variables %1 through %9. RewriteRules and RewriteConds can make use use of all of these back-references, due to the fact that the RewriteRule pattern match is evaluated before any RewriteConds are executed.

It's not clear from your post what the significance of the bolded "/siteB_v10/" path-part is. It does not appear necessary to use a RewriteCond for your application, as the RewriteRule itself can check the full requested URL-path as long as the code is located at "/.htaccess" rather than in a subdirectory below that. If the code is located in a subdirectory, then things get a bit more complicated, requiring a check of THE_REQUEST and the setting and testing of a "user-variable" (see RewriteRule [E=var:val] flag) to prevent an 'infinite' rewriting loop.

That's the extent of the useful comments I can make without seeing a code example and a more-thorough description of the goal, the relevant URL-paths and file-paths, and the exact problem with the attempted coded solution.

Jim

Roan

3:07 pm on Sep 12, 2009 (gmt 0)

10+ Year Member



Hello Jim, thanks for the response, very informative.. I'd not appreciated: "last-matched RewriteCond are back-referenced" ... "due to the fact that the RewriteRule pattern match is evaluated before any RewriteConds".

I'm sorry for the lack of clarity., entirely due to the fact I'm struggling somewhat. The bolded subdirectory name I think was because it's going to be highly variable (though not on a GET request by GET request basis) only on a 'as archived' basis.. if... that is any clearer.

The .htaccess is at root level and it is being processed, I've established that.

I'm going to take away what you've said if you don't mind, as I feel I understand something I didn't before, and revisit my .htaccess experiments. See if I can achieve my aim without taking up any more of your time. Either way, I'll come back and post.. whether success or failure and will welcome comments on either outcome.

Many regards
Roan

jdMorgan

3:39 pm on Sep 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, the solution depends on precisely *where* the "/siteB_v10/" comes from -- i.e. whether it is a requested hostname (loosely, the requested "domain name"), a value stored client-side in a cookie, or something else. The value will "have to come from somewhere" as mod_rewrite cannot simply "invent it" out of thin air. So you probably will have to use a RewriteCond to check the relevant variable (HTTP_HOST or HTTP_COOKIE in my two examples here).

If the "/siteB_v10/" value is tucked away in some "session-management" system, then things are likely to get complex and require a scripted solution.

Jim