jroberts
jroberts

Reputation: 449

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]

When we visit http://www.example.com/v2sc

We are being redirected to http://www.example2.comv2sc

We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname

Any ideas on how we can accomplish this?

Thank you!

Upvotes: 1

Views: 36

Answers (1)

noodl
noodl

Reputation: 17408

It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.

RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]

Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.

Upvotes: 1

Related Questions