我需要在不同域上具有相同 URL 結構的多域站點上重定向大約 300 個 URL。例如:
https://www.example.com/de/products.html需要重定向到https://www.example.org/de/products.html
所以我通常的方法不起作用:
RedirectMatch 301 /de/products.html$ /de/products.html
我需要類似的東西
RedirectMatch 301 https://www.example.com/de/products.html$ https://www.example.org/de/products.html
這顯然不起作用,或者我只是沒有開始作業。
不確定是否重要,但它是一個 TYPO3 實體。
uj5u.com熱心網友回復:
mod_aliasRedirectMatch指令僅與 URL 路徑匹配。要匹配主機名,您需要使用帶有附加條件(RewriteCond指令)的 mod_rewrite 來檢查HTTP_HOST服務器變數(HostHTTP 請求標頭的值)。
此外,由于兩個域的 URL 結構相同,因此您只需要一個規則 - 只需使用初始請求中的相同 URL 路徑即可。無需像您試圖做的那樣進行一對一的重定向。
例如,在任何現有的重寫之前,以下內容需要放在.htaccess檔案的頂部:
RewriteEngine On
# Redirect everything from example.com to example.org and preserve the URL-path
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^ https://www.example.org%{REQUEST_URI} [R=301,L]
這將檢查example.com和www.example.com。
服務器變數已經包含一個斜杠前綴,因此在替換字串REQUEST_URI中省略了它。
首先使用 302(臨時)重定向進行測驗,以避免潛在的快取問題。
更新:
但我不想重定向所有的 URL,只是一些。
根據您的原始示例,將特定 URL 重定向到目標域中的相同 URL:
# Redirect "/de/product.html" only
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^de/products\.html$ https://www.example.org/$0 [R=301,L]
以上僅重定向https://www.example.com/de/products.html到https://www.example.org/de/products.html.
$0反向參考包含RewriteRule 模式匹配的整個 URL 路徑。
如何使用
/de/或/fr/等擴展您的代碼段?例如我想重定向example.com/de/products.html但不是example.com/products.html
也許上面的例子就是你所需要的。或者,要僅重定向/de/<something>(或/fr/<something>)而不只是重定向/<something>,您可以執行以下操作:
# Redirect "/<lang>/<something>" only, where <lang> is "de" or "fr"
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^(de|fr)/[^/] $ https://www.example.org/$0 [R=301,L]
以上將重定向https://example.com/de/<something>到https://www.example.org/de/<something>.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/483530.html
