RedirectPermanent 與 RewriteRule

RedirectPermanent 與 RewriteRule

我目前有一個 perm_redirects.conf 文件,該文件包含在我的 apache 配置堆疊中,其中的行格式為

RedirectPermanent /old/url/path /new/url/path

看起來我需要為新路徑使用絕對 URL,例如: http://example.com/new/url/path。在我得到的日誌中「不完整的重定向目標 /new/url/path 已更正為http://example.com/new/url/path」。(釋義)。

在 2.2 文件中RewriteRule,底部顯示以下內容為有效重定向,重定向右側僅包含 url 路徑,而不是絕對 URL:

RewriteRule ^/old/url/path(.*) /new/url/path$1 [R]

但我似乎無法使用該格式來複製該RedirectPermanent版本的功能。這可能嗎?

答案1

波形調頻:

RewriteEngine On
RewriteOptions Inherit 
RewriteRule ^/old/url/path(.*) /new/url/path$1 [R]

結果:

wget http://localhost/old/url/path/foo
--2010-08-09 15:41:20--  http://localhost/old/url/path/foo
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://localhost/new/url/path/foo [following]
--2010-08-09 15:41:20--  http://localhost/new/url/path/foo

注意 (1) 您可能需要 [R=permanent]。預設是暫時重定向。 (2) 您不需要只使用 [L],因為這將導致最終使用者不可見的內部重定向(不是由 L 引起的,預設操作「」是內部重定向)。 RedirectPermanent 向最終用戶產生 3xx 回應。您可以新增 L,即 [R=permanent,L] 來阻止 mod_rewrite 處理更多規則。

相關內容