Skye Lans
Skye Lans

Reputation: 41

.htaccess redirect multiple domain to single domain

I'm trying to have all webpages from an old domain go to the corresponding webpages on a new domain with the same file structure. I'm using the following code but the problem is the old webpage goes to the index webpage and not the corresponding webpage. Basically the (.*) which I think captures the rest of the url isn't being displayed with the $1 in the rewrite rule?

 Options +FollowSymLinks -MultiViews
 RewriteEngine On
 RewriteBase /
 RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com(.*) [NC]
 RewriteRule ^ http://www.newdomain.com$1 [R=301,L]

Upvotes: 0

Views: 314

Answers (1)

showdev
showdev

Reputation: 29188

If you use $1, you'll need to capture something in your RewriteRule using (). Try this:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

This redirects http://olddomain.com/page.php to http://www.newdomain.com/page.php.

Also, in case there can be multiple olddomains, redirect any url that is NOT at newdomain:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?newdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

This redirects http://notnewdomain.com/page.php to http://www.newdomain.com/page.php.

EDIT:

Here's a screenshot of my test at http://htaccess.madewithlove.be: tested at htaccess.madewithlove.be

Upvotes: 1

Related Questions