2015-03-30 117 views
0

我使用的.htaccess採取這一看起來像URL:的.htaccess重寫規則,友好的URL重定向+

/index.php?page=2 

,並將其重寫,例如:

/contact-us 

我想做兩件事:

當加載/聯繫我們,顯示頁面/index.php?page=2但保持友好的URL。我知道這看起來像這樣:

RewriteRule ^contact-us$ "index\.php\?page\=2" [L] 

哪個工作正常。但是現在我還希望導航到/index.php?page=2的用戶最終接觸/聯繫我們 - 我如何將301重定向與友好的URL重寫結合起來實現此目標?

回答

0

不確定爲什麼您在重寫規則的目標部分中使用引號和轉義。目標部分不是正則表達式。這可以看起來像:

RewriteRule ^contact-us$ /index.php?page=2 [L] 

要重定向index.php?page = 2,您需要執行以下操作。這個規則必須在上面的規則之前,否則你將進入重寫循環。

RewriteCond %{REQUEST_URI} ^/index\.php$ 
RewriteCond %{QUERY_STRING} ^page=2$ 
RewriteRule .* /contact-us [R=301,L] 

這裏要設置兩個重寫條件:該頁面是/index.php和查詢字符串是page=2(只有page=2)。 R=301標誌將強制外部重寫到/ contact-us,併發送HTTP 301標頭。 ?

+0

'重寫規則^指數\ PHP頁面= $ 12 /癮治療恢復的程序[R = 301,L] 重寫規則^ addiction-treatment-recovery-programs $ index.php?page = 12 [L]'這看起來是否正確? – 2015-03-30 17:52:53

+0

@SarahBee看到我最新的更新。我最初沒有正確顯示重寫規則。 – 2015-03-30 17:53:53

+0

'的RewriteCond%{REQUEST_URI}^/索引\ .PHP $ 的RewriteCond%{QUERY_STRING} ^頁= 12 $ 重寫規則。* /成癮治療恢復的程序[R = 301,L] 的RewriteCond%{HTTP_HOST }^edgewood.ca $ [OR] RewriteCond%{HTTP_HOST}^www \ .edgewood.ca $ RewriteRule^addiction-treatment-recovery-programs $ index.php?page = 12 [L]' 對不起,有一點失敗格式與迷你markdown。我現在有了這個和一個重定向循環。還有它重定向與查詢字符串(/ addiction-treatment-recovery-programs?page = 12) – 2015-03-30 18:00:43

0

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代碼:

RewriteEngine On 
RewriteBase/

RewriteCond %{THE_REQUEST} /index\.php\?page=2[&\s] [NC] 
RewriteRule^contact-us? [R=302,L,NE] 

RewriteRule ^contact-us/?$ index.php?page=2 [L,QSA,NC]