Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is there a way to get back references for Rewrite Conditions to be passed to all subsequent rewrite rules?

Here's an example:

RewriteEngine On    
RewriteCond %{HTTP_HOST} (bob).localhost [NC]    
RewriteRule ^/where/? /index.php\?user_name=%1
RewriteRule ^/who/? /index.php\?user_name=%1 

In this example I would expect this behavior:

http://bob.localhost/where => http://bob.localhost/where/index.php?user_name=bob
http://bob.localhost/who   => http://bob.localhost/who/index.php?usern_ame=bob

But for the second rule I receive *http://bob.localhost/who/index.php?user_name=* instead.

I've tried this on several different distro's using Apache 2.2.17

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Conditions only apply to the very next rule. You'll need to repeat the condition in order for it to apply to another rule:

RewriteCond %{HTTP_HOST} (bob).localhost [NC]    
RewriteRule ^/where/? /index.php\?user_name=%1
RewriteCond %{HTTP_HOST} (bob).localhost [NC]    
RewriteRule ^/who/? /index.php\?user_name=%1 

In your example, if your request's HTTP_HOST value doesn't match (bob).localhost, your request will skip the ^/where/? rule (even if it matches), but can use the ^/who/? rule.

share|improve this answer

As DerfK indicates, rewritecond is for only one rewrite rule.

You might consider an alternate strategem however, setting and reading environment variables.

# this grabby rewrite will match anything, 
# *and* set 'bob' in a custom rewrite environment variable 
# it uses 'next' with the 'no sub requests' caveat to avoid loops 
RewriteCond %{HTTP_HOST} (bob).localhost [NC]
RewriteRule ^(.*)$ - [env=host_uname:%1] [N,NS]

# All these rules should then be evaluated, 
# in the 'next' pass - with the 'host_uname' env variable available   
RewriteRule ^/where/? /index.php\?user_name=%{ENV:host_uname}
RewriteRule ^/who/? /index.php\?user_name=%{ENV:host_uname} 
share|improve this answer
    
Aha, I did try that and couldn't it to work. Although your example is different than what I tried. I'll have to try it also. Thank you. –  frogstarr78 Apr 28 '11 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.