views:

28

answers:

1

I want to rewrite from http://example.com/blah/<something>/<somethingelse> to http://<something>.example.com/<somethingelse>, but only if the request is not an internal redirect. How can I achieve this effect? I know I can use %{THE_REQUEST}, but I can't seem to find any good examples.

Thanks for the help!

+1  A: 

The no subrequest flag ([NS]) will skip rules on internal sub-requests.

RewriteCond %{HTTP_HOST} =example.com
RewriteRule ^/?blah/(.*{]) http://something.example.com/$1 [NS,L]
outis
"This webpage has a redirect loop." Nope :(
Cyclone
I added these flags to my rule: [R,L,QSA,NS]
Cyclone
@Cyclone: what other rewrite rules do you have? Since the above rule redirects from "example.com" to "something.example.com", it won't create a loop on its own. Also, it creates an external redirect. You don't need [R] because the substitution is an absolute URL. You don't need [QSA] since you're not setting a query string.
outis
I got it working for http://example.com/blah/something, but when I add a / on to try the redirect it fails
Cyclone
@Cyclone: where are you adding the slash?
outis
The rule, because right now all I have is a redirect for example.com/blah/something, I still need it to work for example.com/blah/something/ and anything after the last /
Cyclone
@Cyclone: obviously it's in the rewrite rule, but where precisely in the rule? Show me the rules you have; edit your question and post them there.
outis
http://pastebin.cyclonehost.info/?paste=299 It is a bit confusing in there. Password is the name of this site, but all lowercase. `#Old format` is where I am having troubles.
Cyclone
@Cyclone: at this point, you're asking a new question (something like "how can I produce canonical URLs without redirect cycles?"), so you should open a new one. Make sure you include your rewrite configuration in your new question, rather than simply posting a link to it.
outis
Alright, I'll probably open a new question if I have not figured it out by then. I'm not seeing any reason why it would loop, but clearly there is an issue. Thank you for your help!
Cyclone
@Cyclone: look to your external redirects.
outis
I fixed it actually, I just added a condition to check for domain being the root domain and it worked.
Cyclone
@Cyclone: excellent. On another note, the pastebin-ed configuration could be simplified in other ways. I might post as a comment here a pastebin link to a partial rewrite, if I have a little spare time.
outis
I know it is confusing and a little more complex than it should be, but I have to support links to the older versions. I will probably do a full rewrite of the file, for efficiency, but unless these rewrites induce lag I think I will be fine. I am not going to make any major changes to my system at this point.
Cyclone