2016-05-23 74 views
0

有與查詢字符串重定向建立共同的規則

RewriteCond %{QUERY_STRING} ^PAGEN_1=1 [NC] 
RewriteRule ^news/$ http://www.mysite.ru/news/$ [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=1 [NC] 
RewriteRule ^news/$ http://www.mysite.ru/news/$ [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=2 [NC] 
RewriteRule ^news/$ http://www.mysite.ru/news/?PAGEN_1=2 [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=3 [NC] 
RewriteRule ^news/$ http://www.mysite.ru/news/?PAGEN_1=3 [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=4 [NC] 
RewriteRule ^news/$ http://www.mysite.ru/news/?PAGEN_1=4 [R=301,L] 


RewriteCond %{QUERY_STRING} ^PAGEN_1=1 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/ [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=1 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/ [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=2 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/?PAGEN_1=2 [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=3 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/?PAGEN_1=3 [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=4 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/?PAGEN_1=4 [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=5 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/?PAGEN_1=5 [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=6 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/?PAGEN_1=6 [R=301,L] 

頁幾個重定向規則有什麼辦法來寫這些規則的共同點,而不是爲每個查詢字符串?

UPD 我用這個代碼

RewriteCond %{QUERY_STRING} ^PAGEN_1=1 [NC] 
RewriteRule ^news/$ http://www.mysite.ru/news/$ [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=1 [NC] 
RewriteRule ^news/$ http://www.mysite.ru/news/$ [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=([0-9]*)$ [NC] 
RewriteRule ^news/(.*)$ http://www.mysite.ru/news/?PAGEN_1=%1 [R=301,L] 




RewriteCond %{QUERY_STRING} ^PAGEN_1=1 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/ [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=1 [NC] 
RewriteRule ^articles/2/$ http://www.mysite.ru/articles/2/ [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=([0-9]*)$ [NC] 
RewriteRule ^articles/2/(.*)$ http://www.mysite.ru/articles/2/?PAGEN_1=%1 [R=301,L] 

,但我不知道這件事。它的工作原理,但我想也許它應該更簡單。

回答

1

您可以使用:

RewriteCond %{QUERY_STRING} (?:^|&)PAGEN_1=1(?:&|$) [NC] 
RewriteRule ^news/?$ http://www.mysite.ru/news/? [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=(\d+)(?:&|$) [NC] 
RewriteRule ^news/?$ http://www.mysite.ru/news/?PAGEN_1=%1 [R=301,L] 

RewriteCond %{QUERY_STRING} (?:^|&)PAGEN_1=1(?:&|$) [NC] 
RewriteRule ^articles/(\d+)/?$ http://www.mysite.ru/articles/$1/? [R=301,L] 

RewriteCond %{QUERY_STRING} ^&PAGEN_1=(\d+)(?:&|$) [NC] 
RewriteRule ^articles/(\d+)/?$ http://www.mysite.ru/articles/$1/?PAGEN_1=%1 [R=301,L] 
+0

我試圖用你的代碼,但我得到的錯誤ERR_TOO_MANY_REDIRECTS – Heidel

+0

'HTTP:// www.mysite.ru'是同一個域?你爲什麼試圖重定向到同一個網站。這是因爲你在'?'之後加了一個'&'? – Croises

+0

我編輯了我的代碼... – Croises