将 https://www.website.com 重定向到 https://website.com 不起作用

问题描述 投票:0回答:3

我发表以下关于将

https://www.website.com
重定向到
https://website.com
的帖子:

Let's Encrypt 证书问题:https://www.website.com 无法重定向到 https://website.com

我无法实现此重定向,我不明白原因是什么。

如果我输入

https://www.website.com
,它会保留在
https://www.website.com
并且不会执行到
https://website.com
的重定向。

我的配置对于使用 Apache2 的 Zope 服务器来说有点特殊。

目前,下面是我的重写规则(

http://www.website.com
http//website.com
都很好地重定向到
https://website.com
):

<VirtualHost *:443>

    # REWRITE to get https://www.website.com to https://website.com except for cgi-bin scripts
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/cgi-bin/search [NC]
    RewriteCond %{REQUEST_URI} !^/cgi-bin/awstats [NC]
    RewriteRule ^/(.*)  https://localhost:8443/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]

</VirtualHost>

<VirtualHost *:80>

<IfModule mod_rewrite.c>

# www to non www for HTTP and HTTPS
RewriteCond %{REQUEST_URI} ^/www\. [NC,OR]
RewriteCond %{REQUEST_URI} !^/podcast [NC]
# Rewrite below works : redirect 80 => https
RewriteRule ^/(.*) https://website.com/$1 [R=301,L]

RewriteRule ^/(.*)  http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]

</IfModule>

</VirtualHost>

这里可能出了什么问题?

apache redirect mod-rewrite url-rewriting
3个回答
0
投票

我相信有一些值得注意的事情

  1. 一个或多个 RewriteCond 可以位于 RewriteRule 指令之前。仅当 URI 的当前状态与其模式匹配并且满足这些条件时,才会使用以下规则。

  2. RewriteRule 指令......可以出现多次,每个实例定义一个重写规则。定义这些规则的顺序很重要 - 这是它们在运行时应用的顺序。

  3. 在 VirtualHost 上下文中,模式最初将与主机名和端口之后、查询字符串之前的 URL 部分进行匹配(例如“/app1/index.html”)。这是(% 解码的)URL 路径。

  4. 如果您希望匹配主机名、端口或查询字符串,请分别使用带有 %{HTTP_HOST}、%{SERVER_PORT} 或 %{QUERY_STRING} 变量的 RewriteCond。

您还可以使用 L|last 标志,这使得引擎在

RewriteRule
运行后停止处理进一步的规则。

上面的内容让您了解引擎如何运行您的重写:

  1. RewriteRules
    按顺序处理(除非您指定
    L
    标志,您对所有规则都这样做)
  2. 每个
    RewriteRule
    可以有多个
    RewriteCond
    ,在考虑
    RewriteRule
    之前所有这些都必须匹配。这也意味着必须为每个
    RewriteCond
    单独重复
    RewriteRule
    (然而,有一种有趣的技术可以将 RewriteRules 分组为 if-then-else 块)
  3. 在您的情况(
    VirtualHost
    上下文)中,默认情况下仅匹配URL路径,除非您手动匹配
    HTTP_HOST
    变量。

考虑到这一点,我发现您的重写规则存在一些问题(为了可读性,我交换了 http 和 https 虚拟主机,但这并不重要):

<VirtualHost *:80> <!-- your http:// requests will end up with this block, because 80 is the port for http -->
<IfModule mod_rewrite.c>
    # www to non www for HTTP and HTTPS <!-- I believe HTTPS traffic is NOT handled by this vhost at all, it arrives straight to *:443 queue -->
    RewriteCond %{REQUEST_URI} ^/www\. [NC,OR] <!-- you evaluate path component of your request uri to begin with "www." (i.e. /www.index.html) - this will obviously never match, you however have OR flag, which proceeds to the second condition -->
    RewriteCond %{REQUEST_URI} !^/podcast [NC] <!-- you check if the path does not start with "/podcast" - this is easy to test - try http://www.website.com/podcast and see if you get redirected to HTTPS - I suspect you will not -->
    # Rewrite below works : redirect 80 => https <!-- I suspect it works by accident, please test it out with http://www.website.com/podcast to confirm my theory -->
    RewriteRule ^/(.*) https://website.com/$1 [R=301,L] <!-- we end up here, and regardless of the requested path we issue a 301 redirect to https version of the website. This is marked as Last rule, so the engine should stop processing here -->
    RewriteRule ^/(.*)  http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L] <!-- this I believe kicks in when you request a "/podcast" path - this will proxy the request to your http://localhost:9674/ -->
</IfModule>
</VirtualHost>

<VirtualHost *:443><!-- this is where your 301 redirect will com after bouncing through first set of rules above -->
    # REWRITE to get https://www.website.com to https://website.com except for cgi-bin scripts
    RewriteEngine On <!-- this is important, keep it on -->
    RewriteCond %{REQUEST_URI} !^/cgi-bin/search [NC] <!-- you check whether url path does not contain /cgi-bin/search -->
    RewriteCond %{REQUEST_URI} !^/cgi-bin/awstats [NC]<!-- AND does not contain /cgi-bin/awstats-->
    RewriteRule ^/(.*)  https://localhost:8443/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]<!-- if both conditions above are met - proxy the request to backend and stop further processing. -->
</VirtualHost>

据我所知 - 没有规则可以重写

https://www.website.com -> https://website.com
,你的 https 重写唯一要检查的是
/cgi-bin

我的建议如下(这可能不是复制粘贴解决方案,但希望您能明白要点):

<VirtualHost *:443>    
    RewriteEngine On
    # www to non www for HTTPS
    <!-- checking for the same thing again -->
    RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
    RewriteRule ^/(.*) https://website.com/$1 [R=301,L] <!-- some people might argue second redirect here is excessive since you already arrived at correct host, but I'd leave this for you to sort out -->
    <!-- your /cgi-bin checks can be merged into one regex --> 
    RewriteCond %{REQUEST_URI} !^/cgi-bin/(search|awstats) [NC]
    RewriteRule ^/(.*)  https://localhost:8443/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]
</VirtualHost>

<VirtualHost *:80>
<IfModule mod_rewrite.c>
    # www to non www for HTTP
    <!-- if you want to keep your `/podcast` on http check it first -->
    RewriteCond %{REQUEST_URI} !^/podcast [NC]
    RewriteRule ^/(.*)  http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]
    <!-- everything else will get redirected to https -->
    RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
    RewriteRule ^/(.*) https://website.com/$1 [R=301,L]
</IfModule>
</VirtualHost>

0
投票

我也遇到了同样的问题,许多网站中只有一个网站虚拟托管在亚马逊 Lightsail Bitnami 实施上。

注释掉该行后

ServerName mywebsite.com:80

在 /opt/bitnami/apache2/conf/httpd.conf 文件中问题已解决。


0
投票

任何人都可以指导我如何重定向我的网站https://techrytr.inhttps://www.techrytr.in/

© www.soinside.com 2019 - 2024. All rights reserved.