Apache 重寫規則互相破壞?

Apache 重寫規則互相破壞?

我有這個規則:

RewriteCond %{REQUEST_URI} ^/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/.*$
RewriteCond %{REQUEST_URI} !^/guide/(.*)$
RewriteRule ^(.*)$ /home/neezer/public-html/domain.com/guide/$1 [L]

它本身效果很好。本質上,我有一堆目錄,其中有一堆文件,我想將它們保留在「/guide」資料夾中,但出於 SEO 原因,我希望它們出現在 Web 根目錄中。

此規則有效,但不幸的是原始 URL 仍然有效(使用“/guide”)。我想將 URL 中帶有“/guide”的內容 301 重定向到不帶“/guide”的內容,而不實際移動伺服器上的檔案。

我嘗試新增這條規則:

RewriteCond %{REQUEST_URI} ^/guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/.*$
RewriteRule ^guide/(.*)$ http://www.domain.com/$1 [R=301,L]

……但這完全違反了我的第一條規則。

關於我可能做錯了什麼有什麼想法嗎?如果您需要向我了解任何其他資訊來幫助我解決此問題,請告訴我。


編輯:根據馬修斯的建議,我已將我的規則更改為(反映我想要的行為):

RewriteRule ^guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ http://www.domain.com/$1-apartments/$2 [R=301,L]
RewriteRule ^(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ /home/neezer/public-html/domain.com/guide/$1-apartments/$2 [L]

然而,規則仍然在相互打破,只是這次以不同的方式。嘗試存取受第二條規則(以及第一條規則正確重定向到的頁面)管轄的頁面在 Safari 中出現以下錯誤:

嘗試開啟時發生太多重定向“http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php」。如果您開啟的頁面被重新導向以開啟另一個頁面,然後該頁面再次重新導向以開啟原始頁面,則可能會發生這種情況。

有什麼建議麼?


進一步編輯:

這些規則似乎正在自我循環。以下是重寫日誌的摘錄:

init rewrite engine with requested uri /manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
pass through /manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
[perdir /home/neezer/public-html/domain.com/] rewrite 'manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php' -> '/home/neezer/public-html/domain.com/guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php'

[perdir /home/neezer/public-html/domain.com/] strip document_root prefix: /home/neezer/public-html/domain.com/guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php -> /guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
[perdir /home/neezer/public-html/domain.com/] internal redirect with /guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php [INTERNAL REDIRECT]
init rewrite engine with requested uri /guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php

pass through /guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
[perdir /home/neezer/public-html/domain.com/] rewrite 'guide/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php' -> 'http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php'
[perdir /home/neezer/public-html/domain.com/] explicitly forcing redirect with http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php
[perdir /home/neezer/public-html/domain.com/] escaping http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php for redirect
[perdir /home/neezer/public-html/domain.com/] redirect to http://www.domain.com/manhattan-apartments/east-village-chinatown-lower-east-side-apartments.php [REDIRECT/301]

我相信問題出在中間部分(我用換行符號分隔)。似乎在 Apache 重寫到本地路徑(使用/home/neezer/...)之後,它發出了另一個請求,其中文檔根被剝離,其中當然包括/guide,然後在第一個規則中進行過濾,最終在第二個規則中進行過濾,如此往復無窮大。

我該如何告訴它在第一條規則之後停止?添加[L]好像沒有什麼效果。

答案1

我不確定你為什麼要使用 RewriteConds。在我看來,你可以將所有內容直接放入 RewriteRules 中。您的問題似乎源自於您正在重定向與 Request_URI 相符的所有內容。如果您在 RewriteRule 本身中進行匹配,則重寫的規​​則將不再匹配下一個規則。

您可能想嘗試以下操作:

RewriteRule ^guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ http://www.domain.com/$2 [R=301]
RewriteRule ^(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ /home/neezer/public-html/domain.com/guide/$2 

您可能需要調整這些,因為我不明白您想要完成的所有事情。例如下面的 RewriteCond 似乎是多餘的,所以我把它省略了。如果您確實嘗試重寫指南目錄下的所有內容,則必須將其重新合併。

RewriteCond %{REQUEST_URI} !^/guide/(.*)$

編輯
在我的伺服器上進行一些快速測試後,我能夠使這些規則起作用,但不能在每個目錄上下文中(即不在 .htaccess 中或在 .htaccess 中<Directory></Directory>)。

我沒有對此進行測試,但您也許可以透過使用第二條規則來設定環境變數然後在第一條規則上進行測試來解決此問題。

RewriteCond %{ENV:REDIRECT} !^NO$
RewriteRule ^guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ http://www.domain.com/$2 [R=301]
RewriteRule ^(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$  [E=REDIRECT:NO]

答案2

我無法找到無限循環,所以我將檔案移至一個名為“neighborhoods”的新資料夾並使用以下規則:

RewriteRule ^guide/(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ http://www.domain.com/$1-apartments/$2 [R=301,L]
RewriteRule ^(manhattan|queens|westchester|new-jersey|bronx|brooklyn)-apartments/(.*)$ /home/neezer/public-html/domain.com/neighborhoods/$1-apartments/$2 [L]

不太理想,因為現在我有兩個資料夾(我還有其他文件在指南之外提供),但它確實解決了我的 SEO 問題。

相關內容