我想知道是否有任何方法可以進行重定向,例如
Redirect 301 https://www.mypage.com/test https://www.mypage.com
作為背景:我需要這個,因為我有一個包含 3 種不同語言的網站,并且每種語言在不同的域上運行。如果我正在使用相對路徑進行 /test,它將影響我的每個域,但我只想對一個特定域進行重定向。
正如我在示例中所示,我正在嘗試它,但它不再起作用。
我也在為我的 apache 指令使用 RewriteCond 嘗試它,但它也不適用于絕對路徑
uj5u.com熱心網友回復:
Redirect 301 https://www.mypage.com/test https://www.mypage.com
mod_aliasRedirect指令僅與 URL 路徑匹配。
您需要使用 mod_rewrite在條件(指令)中Host使用HTTP_HOST服務器變數檢查標頭。例如:RewriteCond
RewriteEngine On
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^test$ https://www.example.com/ [R=302,L]
RewriteRule 模式(第一個引數)是一個僅匹配 URL 路徑的正則運算式(類似于Redirect指令,除了在 中使用時沒有斜杠前綴.htaccess)。
以上將發出 302(臨時)重定向從https://www.example.com/test(HTTP 或 HTTPS)到https://www.example.com/.
如果您要重定向到相同的主機名,那么您不一定需要在替換字串中包含方案 主機名(RewriteRule指令的第二個引數)。例如,以下內容與上述*1相同:
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^test$ / [R=302,L]
(*1除非您已UseCanonicalName On在服務器配置中設定并且ServerName設定為請求的主機名以外的其他內容。)
請注意,上面的內容www.example.com完全匹配(CondPattern=上的前綴運算子使其成為字典字串比較)。
要匹配example.com或www.example.com(帶有可選的尾隨點,即 FQDN),請改用正則運算式。例如:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^test$ https://www.%1/foo [R=302,L]
Where%1是對前面CondPattern中第一個捕獲的組的反向參考(即。example.com)。所以,上面將重定向https://example.com(或www.example.com)并重定向到https://www.example.com/foo(總是www)。
參考:
- https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
uj5u.com熱心網友回復:
在這種情況下,您需要撰寫一個 RewriteCond,如下例所示:
RewriteEngine On
RewriteCond %{REQUEST_URI} /test1
RewriteRule (.*) https://example.com/test1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} /test2
RewriteRule (.*) https://example.com/test2/$1 [R=301,L]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450582.html
標籤:WordPress 阿帕奇 .htaccess 重定向 wpml
上一篇:洗掉后可以使用云形成堆疊名稱嗎?
下一篇:SQL:FULLTEXT速度索引
