2014-10-09 60 views
1

我有問題試圖做301重定向在我的htaccess文件。使用我創建的部分重寫規則時遇到問題。總之,重寫剝離了頁面的擴展,並將查詢?primary-view = city替換爲/ city。這是我的htaccess文件。htaccess奇怪的還原與URL重寫規則和301重定向發生

RewriteEngine On 
# Redirect /page.php to /page 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page)\.php [NC] 
RewriteRule^/%1 [R=302,L] 
# Internally forward /page to /page.php 
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC] 
# Redirect /page.php?primary-view=value to /page/value 
RewriteCond %{THE_REQUEST} \s/+(page)\.php\?primary-view=([^\s]+) [NC] 
RewriteRule^/%1/%2? [R=302,L] 
# Internally forward /page/value to /page.php?primary-view=value 
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC] 

# Redirect /chicago-downtown to /chicago 
Redirect 301 /chicago-downtown to /chicago 

所以重寫後的URL看起來像

#Original url 
example.com/page.php?primary-view=chicago-downtown 
#Url rewritten properly 
example.com/page/chicago 

如果我在example.com/page/chicago類型,然後它工作正常。

#Current bad redirect from example.com/page/chicago-downtown 
example.com/page/chicago?primary-view=chicago-downtown 
#Desired redirect from example.com/page/chicago-downtown 
example.com/page/chicago 

我已經試過的人事物,得出的結論,這是下面的行導致問題與301重定向:如果我example.com/page/chicago-downtown其目前重定向到下面輸入。

# Internally forward /page/value to /page.php?primary-view=value 
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC] 

有一些方法可以讓我重寫我的重寫規則,或者是有我需要使用,以防止這種情況的發生重寫狀態?

回答

0

你不需要Redirect規則。只是chicago-downtown => chicago額外mod_rewrite規則:

RewriteEngine On 

RewriteRule ^(page/chicago)-downtown/?$ /$1 [L,R=302,NC] 

# Redirect /page.php to /page 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page)\.php [NC] 
RewriteRule^/%1 [R=302,L] 

# Internally forward /page to /page.php 
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC] 

# Redirect /page.php?primary-view=value to /page/value 
RewriteCond %{THE_REQUEST} \s/+(page)\.php\?primary-view=([^\s]+) [NC] 
RewriteRule^/%1/%2? [R=302,L] 

# Internally forward /page/value to /page.php?primary-view=value 
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC] 
+0

謝謝你,這沒有工作,但是這是最好的答案嗎?每當我們的SEO團隊想調整它時,我們公司都會不斷改變他們的鏈接。我需要不斷地用這種方法制定重寫規則嗎? – Dom 2014-10-09 17:11:50

+0

你一定要避免混合'mod_rewrite'和'mod_alias'規則。一旦您驗證了所有內容,也將302更改爲301。對於相同的內容也改變鏈接不適合搜索引擎優化的目的。 – anubhava 2014-10-09 17:14:01

+1

相信我,我發牢騷,每次都告訴他們。更高的ups不在乎...... – Dom 2014-10-09 17:14:55

0

這可能是因爲您正在將mod_alias指令與mod_rewrite指令混合在一起。這意味着兩個指令都會以不需要的方式應用於相同的請求。取消Redirect指令(mod_alias)並改爲使用重寫規則。但是,你需要之前你的其他規則來添加規則(即RewriteEngine On後右)

RewriteRule ^chicago-downtown(.*)$ /chicago$1 [L,R=301]