Guy Bowden
Guy Bowden

Reputation: 5107

How to redirect to a different domain EXCEPT when url matches certain patterns

I have the following rule in my .htaccess file that should redirect a request like this:

http://www.olddomain.com/some/url/ -> http://www.newdomain.com/some/url/

unless the path matches something like /images/someimage.jpg

There's a few exceptions like this, built into the rule:

RewriteRule ^(?!images/.*|css/.*|js/.*) http://www.newdomain.com/$1 [R=301,L]

This is behaving as expected when I test using http://htaccess.madewithlove.be/

But, on the live server, $1 is never set. Urls that match the pattern are just redirected to the home page "/"

This is the very first rule of my htaccess file, after RewriteEngine On

Upvotes: 1

Views: 256

Answers (1)

anubhava
anubhava

Reputation: 785196

Replace your rule with this:

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/((images|css|js)/ [NC]
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [R=301,L]

Upvotes: 1

Related Questions