Martijn
Martijn

Reputation: 16123

Reuse groups from htaccess Condition

I need to get the domain of a website into my RewriteUrl, but I somehow can't get it to work:

RewriteCond %{HTTP_HOST} !^www\.example\.(test|com)$ [NC]
RewriteCond %{HTTP_HOST} !(thisdomain|thatdomain) [NC]
RewriteRule ^(.*) http://www.example.%2/$1 [R=301,L]

AFAIK, The %2 should contain the domain, but this redirects to www.example./, no extention.
I've also tried this without the 2nd condition, to no avail. Anyone who can see the mistake?

I start a fresh session each change, the .htaccess is not cached, tested this.

Upvotes: 1

Views: 170

Answers (1)

anubhava
anubhava

Reputation: 786291

To be able to capture value you need to make sure that condition executes first. Try this rule:

RewriteCond %{HTTP_HOST} !^(www|this|that) [NC]
RewriteCond %{HTTP_HOST} \.(test|com)$ [NC]
RewriteRule ^ http://www.example.%1%{REQUEST_URI} [R=302,NE,L]

You can not capture a group when you use !, therefor your %2 has no value. You can take it as a seperate Condition to use it.

Upvotes: 1

Related Questions