使用.htaccess中的HTTPS将旧域重定向到新域



假设我们有,

旧域:http://www.olddomain.com
新域:https://www.newdomain.org

这是我的.htaccess文件数据:

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

有时在重定向时,浏览器会显示安全性错误,使其不受信任

现在,

  1. 如何成功
  2. 如何将其从非www重定向到www

在测试之前更改规则的顺序并清除浏览器缓存:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?olddomain.com$ [NC]
RewriteRule ^ https://www.newdomain.org%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ https://www.newdomain.org%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

最新更新