2012-12-30 227 views
0

我有兩個網址,我正在嘗試重寫,過去... 4-5小時(現在頭痛)。Htaccess將GET請求重寫爲另一個GET請求,另一個GET GET請求使用2個GET變量

我試圖改寫

/arts/tag/?tag=keyword 

/search/art?keywords=keyword 

看着其他的問題,我制定了我重寫這樣

RewriteRule /arts/tag/?tag=([^&]+) search/art?keywords=$1 [L,R=301,NC] 

RewriteRule ^arts/tag/?tag=$ /search/art\?keywords=%1? [L,R=301,NC] 

我嘗試了反斜槓,沒有,沒有運氣。

也試過

RewriteCond %{QUERY_STRING} /arts/tag/?tag=([^&]+) [NC] 
RewriteRule .* /search/art\?keywords=%1? [L,R=301,NC] 

第二個是類似的,

/arts/category?id=1&sortby=views&featured=1 

/art/moved?id=1&rearrange=view 

我改變得變量名是我自己學習的原因目的,因爲我沒有找到任何教程爲我的目的。由於類別已更改,我還將類別更改爲移動,並且我必須在內部重定向某些ID#。

RewriteCond %{QUERY_STRING} id=([^&]+) [NC] // I need the path in there though, not just query string, since I'll be redirecting /blogs/category and /art/category to different places. 
RewriteRule .* /art/moved/id=%1? [L,R=301,NC] 

任何幫助將不勝感激。謝謝。

+0

當追加到查詢字符串時,應該使用'QSA'作爲'RewriteRule'的標誌(QSA代表「Query String Append」)。 – fge

+0

謝謝,我會給它一個鏡頭。 – Darius

+0

像這樣? RewriteRule/arts/tag /?tag = $/search/art?keywords = $ 1 [QSA,NC],仍然沒有。我試着在上面發佈的所有代碼中實施QSA,而不僅僅是那一個。 – Darius

回答

1

假設在原來的網址查詢沒有任何共同之處與那些在替代網址,也許這將做你想做的,使用在查詢中的第key爲條件,並確定輸入的URL:

RewriteEngine On 
RewriteBase/
# First case 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} \btag\b 
RewriteRule .* http://example.com/search/art?keywords=keyword? [L] 

將映射這樣的: http://example.com/arts/tag/?tag=keyword

要這樣: http://example.com/search/art?keywords=keyword

# Second case 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} \bid\b 
RewriteRule .* http://example.com/art/moved?id=1&rearrange=view? [L] 

將映射這樣的: http://example.com/arts/category?id=1&sortby=views&featured=1

要這樣: http://example.com/art/moved?id=1&rearrange=view

兩者都默默的映射。如果新的URL將被顯示在瀏覽器的地址欄中,請修改像這樣的標誌[R,L]。將R替換爲R=301以獲得永久重定向。

+0

謝謝你。對於第一個,如果我專門爲/藝術重寫,並且不想/視頻重寫,我將如何陳述?如果關鍵字是動態的,我會使用$ 1還是%1,「keyword」是動態的。所以要重申一下,我只想在相同的條件下重寫/ in/arts,而不是/ videos或/ blog。 第二種情況也是如此。 謝謝。這工作,只需要看看它是如何動態的。 – Darius

+0

因此,您希望第一個'key'在各個URL中是動態的,只有'/ arts'和'/ category',否則其他任何內容都應該被拒絕。我對嗎?將等待您的評論來更新我的答案。 –

+0

我想/ VIDEOS/tag /?tag =關鍵字保持不變,但是如果是/ ARTS/tag /?tag =關鍵字來處理重寫。此外,「關鍵字」一詞是動態的,所以在重寫時,如果url是/ arts/tag /?tag = hello它應該重寫search/art?keywords = hello關鍵字是動態變量。對於第二個查詢,同樣的事情,GET變量在重寫時應該是動態的。非常感謝您的幫助。你是最棒的! – Darius