Oscar Fanelli
Oscar Fanelli

Reputation: 3677

Htaccess regex: exclude REQUEST_URI

I really tried everything, the tool htaccess tester says that it's ok, but it's not really working.

I want to redirect a subdomain to the main domain only if the REQUEST_URI doesn't start with 3 specific strings.

I tried without success with:

RewriteCond %{HTTP_HOST} ^sub.example.com$ [NC]
RewriteCond %{REQUEST_URI} !(aaa|bbb|ccc)
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [R=301,NC,L,QSA]

I want that:

Thanks

Upvotes: 1

Views: 598

Answers (1)

anubhava
anubhava

Reputation: 785276

Try this rule instead:

RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} !\s/+(aaa|bbb|ccc)/ [NC]
RewriteRule . http://www.example.com%{REQUEST_URI} [R=301,L,NE]

Test it after clearing your browser cache.

Using THE_REQUEST instead of REQUEST_URI to make sure we use unchanged URI values as received by Apache.

Upvotes: 2

Related Questions