【问题标题】:Simple htaccess mod rewrite简单的 htaccess mod 重写
【发布时间】:2016-07-18 18:27:18
【问题描述】:

我有多个以 .nl .com 和 .de 结尾的域。对于每个域名,我都有不同的语言。我的文件位于 .nl 域中。我想要的是所有域的以下内容:

  • 将 http://* 重定向到 https://*
  • http://example.* 重定向到https://www.example.*
  • 我将文件放在 1 台服务器上,并希望它们看起来像在 3 台服务器上。

这是我在主服务器上的 (.NL)

RewriteCond %{HTTP_HOST} !www.example.nl$ [NC]
RewriteRule ^(.*)$ https://www.example.nl/$1 [L,R=301]

这适用于 .DE

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.de/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !www.example.de$ [NC]
RewriteRule ^(.*)$ http://www.example.de/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.nl/$1 [P]

这适用于 .COM

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.nl/$1 [P]

我已经尝试了所有方法,但我无法弄清楚。

【问题讨论】:

  • 先将所有请求重写为https,这样更容易。由于所有内容都将访问 www.example.com,因此只需设置使用 [OR] 的规则: RewriteRule ^(.*)$ example.com/$1 [OR] RewriteRule ^(.*)$ example.de/$1 。我不熟悉 [P] 代理服务器,但您似乎没有重定向到代理服务器,只是重定向到 .nl 域,标准 301

标签: apache .htaccess mod-rewrite


【解决方案1】:
# This rule will redirect users from their original location, to the same location but using HTTPS.
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.example.nl/$1 [R=301,L]

#primary site redirect
RewriteCond %{HTTP_HOST} !^www\.example\.nl [NC]
RewriteRule (.*) https://www.example.nl/$1 [R=301,L]

我相信这就是您所需要的,您首先将所有不是 https 的请求重定向到 https example.nl,然后将任何不是 www.example.nl 的请求重定向到 https://www.example.nl

如果我对 [P] 所需的内容猜错了,您可以将我认为的 [R=301,L] 替换为 [P],尽管我从未处理过该配置,所以我不能说当然可以,但它很容易测试。

https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

这或多或少是我们用于 https 的。我认为您会感到困惑并且没有意识到您需要知道的只是某些东西不是 example.nl/$1,没有必要列出所有这些不是,因为它们都不是 example.nl/$1

注意以下,所有不是第二条规则,都会被重定向:

https://example.nl
https://www.example.com
https://example.de
https://www.example.de

因为这些都不是:

https://www.example.nl

他们被重定向。 (.*) 表示一切,包括 /。

对于 https,您需要注意一件微妙的事情,您必须拥有所有域的所有版本的证书,即 www/非 www,或者您会收到任何初始域/子域的浏览器警报弹出窗口最初加载页面时没有证书支持。例如,如果您没有针对 example.com 的证书,但您有一个针对 www.example.com 的证书,如果 url 进入:http://example.com,您将收到该浏览器警报,这对可用性非常不利,并且会导致它看起来像是一个诈骗网站。

我不记得处理的确切顺序,但我认为它是这样工作的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多