更换域名后的301重定向解决方案(全站)

You can do it with .htacess as suggested by Anthony. I just make it little simpler for you.

Solution 1:

You can use this code to redirect the Old Domain Name to the New Domain Name. (Only domain redirect)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.com [NC]
RewriteRule ^(.*)$ http://new.com/$1 [L,R=301,NC]

Solution 2: If you want to redirect particular Pages as well use the code below. (Don't forget to add RewriteEngine on at the beginning)

# 301 Redirect from OLD to NEW Page
Redirect 301 /old-page https://www.new-domain.com/new-page

You can add multiple redirects to redirect each page like

Redirect 301 /contact-us https://www.new-domain.com/contact-us
Redirect 301 /about https://www.new-domain.com/about
Redirect 301 /blog https://www.new-domain.com/blog

and so on.... Redirect for the root domain to old-domain.com to new-domain.com can be done using

Redirect 301 / https://www.new-domain.com/

Solution 3:

You can also add the following code to redirect the domain as well as the pages to the new domain using the following .htacess code.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.new-domain\.com\/$1" [R=301,L]

Solution 4: You can also do the same using cPanel.

  1. Log in to cPanel and go to the Redirects Option under Domains Check this image for better understanding. cPanel Redirects Option
  2. Add the redirect. Check the Image for better understanding.
  3. Choose the Redirect type. (301 or 302)
  4. Select the domain from the drop-down. (Addon domain will show up too).
  5. do not add anything inside the /.
  6. type your new domain name inside Redirects to
  7. Select www. redirection: option as per your requirement.
  8. Check the Wild Card Redirect box

You are done. cPanel will write the .htaccess code for you.

Remember the solution 3 and Solution 4 will only work if the old domain's filename and new domains file names are same. For example old-domain.com/something will be redirected to new-domain.com/something but it can not be redirected to the new-domain.com/otherthing.

If you have different file name for the new & old domain then use Solution 2 or don't check on the www. redirection: option for Solution 4 and add the page name/file name on the / box (Option number 5).

You can delete all the contents from the addon-domain.com folder using cPanel File Manager or FTP Clients, except the .htacess file to save space. Just keep the .htaccess file. That's enough for you. But always keep a backup copy in case anything goes wrong.

posted @ 2020-05-14 15:29  Zhu8  阅读(360)  评论(0)    收藏  举报