簡體   English   中英

apache2 - https重定向到給定頁面的http

[英]apache2 - https redirection to http for a given page

我有一個特定的問題,關於將HTTP重定向到多個頁面的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/$1 [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 ”)重定向到“ 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/$1 [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/++/$1 [P,L]

根據這些規則,我可以使用HTTPS鏈接瀏覽我的所有網站,但/wiki/test_page.html頁面除外。 為此,我將包含鏈接“ /wiki/test_page.html ”的頁面放入顯式HTTP href鏈接:

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

以這種方式,應用最后一次重寫規則( RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [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/++/$1 [P,L]

是因為Apache2無法檢測到Zope3(8443或443)的HTTPS端口嗎?

現在,我想直接將/wiki/test_page.html上的HTTPS請求/wiki/test_page.html到Zope3服務器上的HTTP請求(位於9674端口上)。 我認為解決方案可能來自修改此重寫規則:

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

我試過例如修改它如下:

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

要么

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

遺憾的是,這不起作用:例如,如果我從包含此鏈接的頁面點擊“ https://website.com/wiki/test_page.html ”鏈接,則不會為上面的RewriteRule重寫URL。

歡迎任何幫助

那么你的vhost文件中的規則看起來很好。 確保在每次更改后重新啟動了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/$1 [R=301,L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM