如果用戶不是來自特定 IP 并且他們正在請求特定域,我需要將用戶重定向到特定頁面。
我試過了 :
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^hello\.example\.com [NC] # the user requested https://hello.example.com
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.99 # the user's IP is not 123.456.789.99
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC] # the user is not already on the redirected page
RewriteRule .* /maintenance.html [R=302,L]
但它不起作用......
幫助?
uj5u.com熱心網友回復:
HTTP_REFERER包含參考URL(HTTP 請求標頭的內容,Referer即用戶來自的 URL ),而不是被請求的域。為此,您需要針對HTTP_HOST服務器變數(即HostHTTP 請求標頭)進行測驗。
另請注意,Apache 不支持行尾注釋 - 您的第二個條件(如所寫)將由于無效的標志引數而導致 500 內部服務器錯誤。
不需要第三個條件來檢查請求的 URL 是否不是/maintenance.html,因為這可以更有效地在RewriteRule本身中進行檢查。
請嘗試以下操作:
RewriteCond %{HTTP_HOST} ^hello\.example\.com [NC]
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.99$
RewriteRule !maintenance\.html$ /maintenance.html [NC,R=302,L]
請注意,RewriteRule 模式匹配的 URL 路徑不以斜杠開頭。
另外:對于檢查單個 IP 地址,使用字典字串比較(使用前綴運算子)通常更容易/更清晰=,而不是正則運算式。例如:
:
RewriteCond %{REMOTE_ADDR} !=123.456.789.99
:
但是,如果您正在實施一個臨時的“維護中”頁面,那么請考慮發送 503 Service Unavailable 回應。
參考:
https://webmasters.stackexchange.com/questions/55635/website-is-under-maintenance-how-to-restrict-access
請參閱我對以下 SO 問題的回答:如何在 .htaccess 503 重定向中保持網站網址相同
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/495767.html
標籤:.htaccess
