Revo Burisan Revo Burisan - 3 months ago 27
Apache Configuration Question

Using variable in mod_rewrite on redirect

I have following code, that works perfectly but my problem is I dont want to birds hard coded in RewriteRule.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /birds/$1 [L,R=301]


I dont want to add it manually because there are many folders like "birds"

Example:

from

http://domain.com/birds/x.html/


to

http://domain.com/birds/x.html


but in my case it returns to

http://domain.com/x.html

Answer

You can capture full URI path from RewriteCond %{REQUEST_URI} instead of RewriteRule which is relative to current directory:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(/.*)/$
RewriteRule /$ %1 [L,R=301,NE]