Ad

Redirect Each Page Of Old Domain To Same Page URL On The New Domain

- 1 answer

I know this question has been asked with all variations but I still cannot find the exact answer. I have changed my domain name. All website files and urls are still the same. old domain still points so the same server and I have the following code in htaccess file.

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

Problem is that this only redirects the old domain home page. What can I do so I don't have to add individual redirects for each page.

Ad

Answer

It sounds like you've put these directives in the wrong place in your .htaccess file. They need to go at the beginning of the file, not at the end.

These directives look typical of cPanel generated redirects. cPanel always places redirects at the end of the .htaccess file (which is often incorrect).

If you have a front-controller before this (such as that used by WordPress) then you will find that these directives will only get processed for requests to the document root (ie. the "homepage"), as all other requests will be stopped by the front-controller.

These directives can also be simplified:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]

I've removed the $ from the end of the CondPattern so as to match a FQDN (that ends in a dot).

There is also no need to backslash escape colons, slashes and dots in the RewriteRulesubstitution, as these characters carry no special meaning here (it's not a regex). (This unnecessary escaping is very typical of cPanel.)

Ad
source: stackoverflow.com
Ad