apache2 - https 重定向到给定页面的 http

apache2 - https redirection to http for a given page

我有一个关于将几个页面的 HTTPS 重定向到 HTTP 的特定问题。

我在 Zope 前面有 Apache2。

这是我的 VirtualHost 在端口 80 上的配置:

<VirtualHost *:80>
    ServerAdmin root@website.com
    ServerName website.com
    ServerAlias www.website.com

<IfModule mod_rewrite.c>
RewriteEngine On

# www to non www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^/(.*) http://website.com/ [R=301,L]

# HTTP TO HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^/(.*) https://%{HTTP_HOST}%{REQUEST_URI}

</IfModule>

</VirtualHost>

之后,当 Zope 监听 SSL 8443 端口时,我应用了以下 iptables 规则:

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8443

所以我的重写规则将 80 重定向到 443,iptables 将 443 重定向到 8443。

在这种情况下,我如何重定向一页(比如“https://website.com/wiki/test_page.html") to "http://website.com/wiki/test_page.html”。

我尝试添加以下规则:

# if https on then turn off
RewriteCond %{HTTPS} on
RewriteRule ^/wiki/test_page.html http://%{HTTP_HOST}%{REQUEST_URI}

但是没用。

与其再做一个重写规则,我认为应该更容易防止页面“/wiki/test_page.html”的HTTPS重定向,但我不知道如何实现这一目标。

欢迎任何建议,

谢谢

更新 1:

我试过了:

# HTTP TO HTTPS except page "/wiki/test_page.html"
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/wiki/test_page.html
RewriteRule ^/(.*) https://%{HTTP_HOST}%{REQUEST_URI}

没有成功

更新 2:

我的问题取得了进展。我的临时解决方案是申请:

RewriteEngine On

# www to non www and HTTP to HTTPS redirection for all website
RewriteCond %{REQUEST_URI} ^/www\. [NC,OR]
RewriteCond %{REQUEST_URI} !^/wiki/test_page\.html [NC]
RewriteRule ^/(.*) https://website.com/ [R=301,L]

# rewrite HTTPS to HTTP for test_page.html
#RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/wiki/test_page\.html [NC]
RewriteRule ^/(.*)  http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/ [P,L]

有了这些规则,除了 /wiki/test_page.html 页面之外,我可以使用 HTTPS links 浏览我的所有网站。为此,我在包含 link “/wiki/test_page.html” 的页面中放入了一个明确的 HTTP href link :

<a class="bottom_link" href="http://website.com/wiki/test_page.html">Test page</a>

这样,应用最后的重写规则(RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/ [P,L]),80端口的请求被转发到Zope3的9674 HTTP端口。

在下面的RewriteCond中,我让你注意到,当我浏览我所有的HTTPS网站时,RewriteCond %{HTTPS} on都不匹配。

# rewrite HTTPS to HTTP for test_page.html
    RewriteCond %{HTTPS} on
    RewriteCond %{REQUEST_URI} ^/wiki/test_page\.html [NC]
    RewriteRule ^/(.*)  http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/ [P,L]

是不是Apache2检测不到Zope3的HTTPS端口(8443或443)?

现在,我想直接将 /wiki/test_page.html 上的 HTTPS 请求转发到 Zope3 服务器(位于 9674 端口)上的 HTTP 请求。我认为解决方案可能来自修改此重写规则:

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

我试着修改如下:

RewriteRule ^/(.*)  http://localhost:9674/++vh++https:%{SERVER_NAME}:443/++/ [P,L]`

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

不幸的是,这不起作用:例如,如果我在包含此 link 的页面上单击 link“https://website.com/wiki/test_page.html”,URL对于上面的 RewriteRule 都没有被重写。

欢迎任何帮助

好吧,我觉得你的虚拟主机文件中的规则没问题。确保在每次更改后重新启动 apache 并清除缓存,因为 301 重定向会被浏览器缓存。您也可以将您的规则组合成 1,如下所示。 应该 有效。

# www to non www and http to https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC, OR]
RewriteCond %{REQUEST_URI} !^/wiki/test_page.html [NC]
RewriteRule ^/?(.*) https://website.com/ [R=301,L]