【问题标题】:Redirect multiple domains to https only仅将多个域重定向到 https
【发布时间】:2018-08-28 06:21:19
【问题描述】:

我有三个域:

  1. example.com
  2. example.net
  3. domain2.example

我希望所有域(www/non-www/HTTPS/non-HTTPS 流量)重定向到 https://www.example.com

我知道我需要先将example.netdomain2.example(www/非www 和HTTPS)重定向到example.com,然后再重定向到https://www.example.com(SSL 证书仅适用于example.comwww.example.com)。

.htaccess(整个文件):

RewriteEngine on
RewriteBase /

RewriteCond !{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]

RewriteCond !{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.example [NC]
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com" [R=301,L]

RewriteCond !{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]

RewriteCond !{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?example.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.example [NC]
RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301,L]

# disable directory listing
Options -Indexes

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

只有example.com 重定向规则似乎工作正常,但example.netdomain2.example 重定向不起作用。这样可以找工作吗?

【问题讨论】:

  • 1) “先到 example.com 然后example.com”你不需要两个链式重定向,我什至建议再次和 2) 你不需要在重定向 URL 中转义字符,它不是一个正则表达式,所以/. 就可以了。 3) 一般来说,对于重定向,我建议在 Apache 主配置中而不是在 .htaccess 中进行重定向,因为这将解决许多问题,您可以使用 Redirect 指令,这些指令足以满足更简单的重定向需求。

标签: .htaccess redirect dns


【解决方案1】:

这就够了:

RewriteCond !{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ 
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

请记住,为了响应 https:// 对其他域的请求,您需要拥有该域名的证书(即使您只想进行重定向,您也需要它!)。

【讨论】:

  • 所以没有证书就不能在没有警告的情况下转移域名来更正一个吗?
  • 是的,完全正确。如果您考虑一下,这是有道理的,如果您没有证书,为什么我的浏览器会信任您的重定向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
相关资源
最近更新 更多