Search code examples
regex.htaccesshttp-redirectmigrationforwarding

How to redirect a URL by only changing the domain name, while keeping other URL parameters


I'm now migrating my website to a new host and domain, and I want to know if I can redirect anyone who enters any URL of the old website to the new website, while keeping all of the URL parameters. for example:

When somebody types in this url http://www.domainA.com/blog/?p=667, I want him to be redirected to http://www.domainB.com/blog/?p=667.

Is there any way I can do that by adding some .htaccess configurations?

Thanks!


Solution

  • Try this in your .htaccess file:

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    
    # for http
    RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
    RewriteCond %{SERVER_PORT} =80
    RewriteRule ^(.*)$ http://www.domainB.com/$1 [R=301,L]
    
    # for https
    RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
    RewriteCond %{SERVER_PORT} =443
    RewriteRule ^(.*)$ https://www.domainB.com/$1 [R=301,L]
    

    This will preserve your URI while redirecting from domainA to domainB.