apache 将www.example.com重定向https://www.website.com到https://website.com无法正常工作

hgb9j2n6  于 2022-11-16  发布在  Apache
关注(0)|答案(2)|浏览(2489)

我在https://www.website.comhttps://website.com的重定向上发表了以下帖子:
Issue with Let's Encrypt certificate : https://www.website.com not working with redirection to https://website.com
我无法实现这种重定向,我不明白是什么原因。
如果我键入https://www.website.com,它将保留在https://www.website.com上,并且不会执行到https://website.com的重定向。
我的配置有点特殊,Zope服务器与Apache2一起工作。
现在,下面是我的重写规则(http://www.website.comhttp//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>

这里有什么问题吗?

0ejtzxu1

0ejtzxu11#

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

  1. RewriteRule指令......可以多次出现,每个示例定义一个重写规则。定义这些规则的顺序很重要-这是在运行时应用这些规则的顺序。
    1.在VirtualHost上下文中,模式最初将与URL中位于主机名和端口之后、查询字符串之前的部分(例如“/app1/index.html”)进行匹配。这是(%解码的)URL路径。
    1.如果您希望匹配主机名、端口或查询字符串,请分别使用带有%{HTTP_HOST}、%{SERVER_PORT}或%{QUERY_STRING}变量的RewriteCond。
    你还利用了L| last标志,这使引擎在运行RewriteRule后停止处理更多规则。
    以上几种方式让您了解引擎是如何运行重写的:
    1.按顺序处理RewriteRules(除非您指定L标志,您将对所有规则执行此操作)
    1.每个RewriteRule可以有多个RewriteCond,在考虑RewriteRule之前,所有的RewriteCond都必须匹配。这也意味着RewriteCond必须对每个RewriteRule单独重复(然而,有一个有趣的技术可以将RewriteRules分组到if-then-else块中)
    1.在您的情况下(VirtualHost上下文),默认情况下仅匹配URL路径,除非您手动匹配HTTP_HOST变量。
    考虑到这一点,我发现重写规则存在一些问题(为了可读性,我交换了http和https vhosts,但这并不重要):
<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>
eoxn13cs

eoxn13cs2#

我也遇到了同样的问题,因为只有一个网站实际上托管在亚马逊lightsail bitnami实现上。
注解掉该行之后

ServerName mywebsite.com:80

在/opt/bitnami/apache 2/conf/httpd.conf文件中,这个问题得到了解决。

相关问题