user1844933
user1844933

Reputation: 3417

.htaccess to redirect to new domain

I would like to redirect my existing domain to new one (bot sites structure are same) I tried following in .htaccess

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

My problem is, If I try

olddomain.com/login.php

it redirects to

newdomain.com/public_html/login.php

which is not working.. Can any one help to fix it.

(Note: If I try only domain name it redirects to new domain perfectly)

Upvotes: 2

Views: 213

Answers (3)

Then you can use this RewriteRule directive:

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

It will only backreference the regex (.*) excluding the "public_html/".


It's nice to see the complete directives that solved your problem. Anyway, you can improve and optimize them by replacing these 3 lines:

RewriteCond %{HTTP_HOST} ^olddomain\.tld$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.tld$
RewriteRule ^(.*)$ "http\:\/\/www\.newdomain\.tld\/$1" [R=301,L]

With these 2 lines:

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

That is the perfect way to write them, in my sight..

Upvotes: 2

user1844933
user1844933

Reputation: 3417

After discussed with service provider, as per their instruction I used following code, which works perfectly (As per their information for Virtual Private Server is special case)

Thanks to all who take efforts to solve my problem

RewriteEngine on
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^olddomain\.tld$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.tld$
RewriteRule ^(.*)$ "http\:\/\/www\.newdomain\.tld\/$1" [R=301,L] 

Upvotes: 1

Chirag Nandaniya
Chirag Nandaniya

Reputation: 495

Just point your new domain to site Root folder, not the your server root folder.

Upvotes: 3

Related Questions