這是我試圖在 httpd.conf 中應用的代碼:
<VirtualHost *:80>
ServerName www.test.com
ServerAlias www.test.com
DocumentRoot /home/test_com
ErrorLog /home/logs/apache/www_test/www.test.com-error_log
CustomLog /home/logs/apache/www_test/www.test.com.-access_log common env=!do_not_log
# https redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} www.test.com
RewriteCond %{REQUEST_URI} /example
RewriteRule . https://www.test.com/example.html [R=303,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}
<Directory "/home/test_com/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
應用代碼并重新啟動 apache 后,當我轉到 /example 時,我收到 404 not found 回應。
當我在 ( https://htaccess.madewithlove.be )測驗 htaccess 時,輸出是我想要的https://www.test.com/example.html。我想知道是什么問題。
uj5u.com熱心網友回復:
RewriteRule 的正確部分是目標檔案,嘗試更改:
RewriteCond %{REQUEST_URI} /example
到 :
RewriteCond %{REQUEST_URI} /example.html
uj5u.com熱心網友回復:
RewriteCond %{HTTP_HOST} www.test.com RewriteCond %{REQUEST_URI} /example RewriteRule . https://www.test.com/example.html [R=303,L]當我轉到 /example 時,我收到 404 not found 回應。
不確定您的示例中是否丟失了某些內容,但是根據所寫的規則,如果您請求/example它將導致重定向回圈,因為它將不斷地/example.html一次又一次地重定向到。(除非您實際上是通過 HTTPS 發出請求,在這種情況下,指令根本不執行任何操作,您確實會得到 404!)
這是因為第二個條件 %{REQUEST_URI} /example也與目標 URL 匹配/example.html。您需要使正則運算式更具體且僅匹配/example,而不是任何僅包含/example. 這個檢查也可以移到RewriteRule指令中(條件不是必需的)。例如:
RewriteRule ^/example$ https://www.test.com/example.html [R=303,L]
檢查 的條件HTTP_HOST似乎沒有必要,因為這些指令在 vHost for 中www.test.com,所以它不能是任何其他主機名?
但是,由于此規則位于埠 80 的 vHost 中,因此它僅適用于 HTTP 請求(而非 HTTPS)。如果您實際上是在請求https://www.test.com/example(即 HTTPS),正如您的 MWL 工具的測驗結果所暗示的那樣,那么該指令將永遠不會被處理,您確實會得到 404。
RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}
您的 HTTP 到 HTTPS 重定向缺少最后一部分,因此將所有內容重定向到主頁。該RewriteRule指令應為:
RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]
但是,如果您的所有指令都在 vHost:80 容器中,則重定向到 HTTPS 毫無意義。
這個規則也比它需要的更復雜。由于您在 vHost:80 容器中,HTTPS因此總是會off- 檢查是多余的。
ServerName www.test.com ServerAlias www.test.com
如果您只有一個主機名,那么您不需要兩個指令。ServerName在這種情況下只需要 。
當我在 (
https://htaccess.madewithlove.be)測驗 htaccess
你.htaccess這里沒有檔案。您直接在 vHost 容器中使用指令,這是一個不同的背景關系。指令在這里的行為略有不同。例如,RewriteRule 模式 .(單個點)在此處的結果與在.htaccess. 在虛擬主機背景關系中,它對每個請求都是成功的,而在除主頁之外的.htaccess所有內容中都是成功的(這可能是這里的意圖)。
However, the MWL .htaccess tester only makes a "single" request (or single pass through the file). It does not "follow" redirects or make multiple passes through the file as it would do in a real server environment. So it cannot catch redirect/rewrite loops - which are a common cause of error.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/316620.html
下一篇:快速重定向
