更新
我找到了這篇文章,它解釋了RewriteRule可以使用.htaccess但需要修改。
這行得通,但它需要成為第二條規則RewriteEngine On
RewriteRule ^/?find-a-home/new-homes/greenbooth/?(.*)$ https://demo.com/new-homes [L]
這適用于這樣的 URL
/find-a-home/new-homes/greenbooth/property-type/bower
但是我將如何修改它以處理帶有查詢字串的 URL?
/find-a-home/new-homes/greenbooth-village/property-types?property_beds=&property_type_price=60000
我已經制定了這個 htaccess 規則
RewriteRule ^/?find-a-home/new-homes/greenbooth/?(.*)$ https://demo.com/new-homes [R=301,L]
我在這里測驗過 https://htaccess.madewithlove.com?share=ebd5828d-9535-4ae9-b74a-1b70a19535a2
htaccess.madewithlove.com 驗證器說它有效。
但是,當我更新demo.com到我的實際url并將它放在我的服務器上時,它不起作用?
我檢查了服務器日志,我可以看到這個錯誤
2022-06-14 10:27:37.449888 [INFO] [515989] [T0] [HTAccess] [/home/wwwsite/public_html/.htaccess:7] Redirect URL [^/find-a-home/new-homes/greenbooth/(.*)] does not fall inside current context [/], ignore.
是什么does not fall inside current context意思,有沒有辦法更新它,RewriteRule所以它確實屬于當前背景關系?
我正在嘗試使用一條規則捕獲多個頁面。我有一堆這樣的頁面
https://demo.com/find-a-home/new-homes/greenbooth/property-types/gilson
https://demo.com/find-a-home/new-homes/greenbooth/property-types/holtham
https://demo.com/find-a-home/new-homes/greenbooth/property-types/howarth
我想捕獲任何包含find-a-home/new-homes/greenbooth并重定向到的urlhttps://demo.com/new-homes
根據我的閱讀,Redirect 301不接受正則運算式 - 這就是我使用 RewriteRule 的原因。盡管感覺我需要添加其他東西才能使其正常作業。
任何人都可以幫忙嗎?
.htaccess檔案。其余規則有效。
# Perch Runway
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/perch
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /perch/core/runway/start.php [L]
# doesn't work, doesn't error?
RewriteRule ^/?find-a-home/new-homes/greenbooth/?(.*)$ https://demo.com/new-homes [R=301,L]
Redirect 301 /index.htm /
Redirect 301 /portfolio.htm /find-a-home/new-homes
Redirect 301 /buyers.htm /
Redirect 301 /land.htm /
uj5u.com熱心網友回復:
RewriteCond %{REQUEST_URI} !^/perch RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* /perch/core/runway/start.php [L] # doesn't work, doesn't error? RewriteRule ^/?find-a-home/new-homes/greenbooth/?(.*)$ https://demo.com/new-homes [R=301,L]
這兩條規則的順序是錯誤的。請求/find-a-home/new-homes/greenbooth/<anything>(我假設它不映射到物理檔案)首先被重寫為/perch/core/runway/start.php. 因此,第二條規則永遠不會匹配并且什么也不做。
通常,外部重定向需要在內部重寫之前進行。
這兩個規則也可以通過幾種方式來簡化。您沒有在替換字串中使用反向參考,因此不需要捕獲子模式(即。(.*))。并且檢查 URL 是否尚未啟動的條件perch應移至RewriteRule 模式(不需要附加條件)。
例如:
# Redirects
RewriteRule ^find-a-home/new-homes/greenbooth https://example.com/new-homes [R=301,L]
# Front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !perch /perch/core/runway/start.php [L]
正則運算式匹配以該字串開頭^find-a-home/new-homes/greenbooth的所有 URL (在 中沒有斜杠前綴)。.htaccess
您應該首先使用 302(臨時)重定向進行測驗,以避免潛在的快取問題。
But how would I modify it to handle URLs with query strings? /find-a-home/new-homes/greenbooth-village/property-types?property_beds=&property_type_price=60000
這取決于你的意思。上述規則已經“處理帶有查詢字串的 URL”,因為它們將被重定向。上面的規則將重定向指定的 URL。但是,默認情況下會在重定向中保留查詢字串。如果要從重定向回應中洗掉查詢字串,則在重定向中包含QSD(查詢字串丟棄)標志。
例如:
RewriteRule ^find-a-home/new-homes/greenbooth https://example.com/new-homes [QSD,R=301,L]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/495775.html
標籤:.htaccess
