如果 URL 不包含子字串,我需要將所有請求重定向
https://example.com/*到。https://example.com/test/*test
到目前為止,我有這些重寫規則
RewriteBase /
RewriteCond %{THE_REQUEST} !^/test
RewriteRule ^/?$ /test/$1 [R=301,L] # if the url does not contain test, redirect to url with test
RewriteCond %{THE_REQUEST}% test
RewriteRule ^test?(.*)$ /$1 [L] # mask the fact that the url is not https://example.com/ and instead is https://example.com/test but apache serve the website like if it was on root
如果我訪問https://example.com它會重定向到https://example.com/test但由于第二條規則而給出無限回圈。
我該如何組合它,所以請求https://example.com/test*不要被重定向,但這些請求https://example.com/*不需要更改 www 根目錄,因此它適用于所有 URL。
uj5u.com熱心網友回復:
更新:
測驗應該在 url 中(為了用戶體驗),但是 apache 應該像不在 url 中一樣路由,而是請求到達根 url,因此應用程式路由在內部保留,而無需更改應用程式本身。
喔好吧。但是,您應該鏈接到/test您的應用程式中的 URL(因此您仍然需要“更改應用程式”,盡管您最后發表了評論),否則/test實際上不在用戶和搜索引擎在頁面上看到的 URL 中(他們將被重定向),并且您的用戶每次單擊您的鏈接之一時都會體驗到外部重定向(對 SEO 和用戶體驗不利)。
.htaccess為“舊” URL 添加前綴的“重定向”/test僅用于 SEO - 與任何“舊”到“新” URL 更改一樣。(您的應用程式不需要/test在 URL 路徑中運行 - 因為您的內部 URL 應該已經包含/test.)
試試這樣:
RewriteEngine On
# Insert "/test" at the start of the URL-path if absent in "direct" requests
RewriteRule %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^test/ /test/$1 [R=301,L]
# Rewrite "/test" URLs back to root
RewriteRule ^test(?:$|/(.*)) /$1 [L]
REDIRECT_STATUS環境變數用于防止重定向回圈。這在來自客戶端的初始請求中為空,并在重寫后設定為 HTTP 回應狀態(如下)。
首先使用 302(臨時)重定向進行測驗,只有在您確定它按預期作業時才更改為 301(永久)。
您需要在測驗前清除瀏覽器快取。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/456032.html
