Bart Platak
Bart Platak

Reputation: 4475

Reuse captured group in RewriteCond

I'm trying to setup a rewrite composed of 2 RewriteCond and one RewriteRule directives. The main purpose is to redirect all requests to css,media and js directories to correspondence subdomains. I have easily achieved it using 3 separate redirects, however I would like to be able to do it using just one.

I have tried

RewriteCond %{REQUEST_URI} ^/(css|media|js)
RewriteCond %{HTTP_HOST} !^%1\.
RewriteRule ^(media|css|js)(/.+)$ http://$1.%{HTTP_HOST}/$1$2 [R=302,L]

However, it doesn't seem to allow the use of capturing group from first RewriteCond inside the other.

Upvotes: 0

Views: 639

Answers (1)

Qben
Qben

Reputation: 2623

I don't think it's working with back referencing from one RewriteCond to another, but you should be able to do:

RewriteCond %{REQUEST_URI} ^/css|media|js
RewriteCond %{HTTP_HOST} !^css|media|js\.
RewriteRule ^(media|css|js)(/.+)$ http://$1.%{HTTP_HOST}/$1$2 [R=302,L]

Hope it help.

Upvotes: 1

Related Questions