user398341
user398341

Reputation: 6577

.htaccess 301 redirect

Does anyone know how can I do a 301 redirect traffic from one domain to the other - including all the same url elements after domain name - and apply the same rule to all possible links without writing it for each url separately - example would be something like this:

http://www.olddomain.com/catalogue/category/fruits/pg/2.html

to redirect to:

http://www.newdomain.com/catalogue/category/fruits/pg/2.html

At the moment I have something like this:

RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http\:\/\/www\.newdomain\.com" [R=301,L]

but this won't work for the following situation for instance:

http://www.olddomain.com/login.html

it simply won't redirect to the new domain and keep the /login.html after.

Any clues?

Upvotes: 1

Views: 1193

Answers (3)

user398341
user398341

Reputation: 6577

Ok - after a lot of googling I've found the solution - which comes to a simple two lines:

RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

Thanks everyone for participating.

Upvotes: 0

nxt
nxt

Reputation: 1983

Youre RewriteRule is incorrect. You're only redirecting http://www.olddomain.com/ but not /login.html

Try modifying the rewrite rule to include all paths on the old domain:

RewriteRule ^(.*)$ http://www.newdomain.com$1 [R=301,L]

Upvotes: 1

Tom Walters
Tom Walters

Reputation: 15616

Have you tried simply:

Redirect 301 / http://newdomain.com/

In the root folder of olddomain.com

Upvotes: 2

Related Questions