RewriteRule - loop redirection

余生颓废 提交于 2019-12-25 07:46:49

问题


i need help with this 2 rewrite rules:

RewriteEngine On
RewriteBase /

# folder/script.php?A=1&B=2  ->  xyz/1/2 (REDIRECT)
RewriteCond %{QUERY_STRING} ^A=([^&]+)&B=([^&]+)$
RewriteRule ^folder\/script\.php$ /xyz/%1/%2? [R=301,L]

# xyz/1/2  ->  folder/script.php?A=1&B=2 (REWRITE)
RewriteRule ^xyz\/([^\/]+)\/([^\/]+)$ /folder/script.php?A=$1&B=$2 [L]
  • First I need "REDIRECT"
    FROM: efectorelativo.net/folder/script.php?A=1&B=2
    TO: efectorelativo.net/xyz/1/2

  • Then i need "REWRITE" not "REDIRECT"
    FROM: efectorelativo.net/xyz/1/2
    TO: efectorelativo.net/folder/script.php?A=1&B=2

EDIT: (working code, thanks to Gumbo)

RewriteEngine On
RewriteBase /

# folder/script.php?A=1&B=2  ->  xyz/1/2 (REDIRECT)
RewriteCond %{THE_REQUEST} \?A=([^&]+)&B=([^\s&]+)
RewriteRule ^folder\/script\.php$ /xyz/%1/%2? [R=301,L]

# xyz/1/2  ->  folder/script.php?A=1&B=2 (REWRITE)
RewriteRule ^xyz\/([^\/]+)\/([^\/]+)$ /folder/script.php?A=$1&B=$2 [L]

回答1:


Inspect the request line in THE_REQUEST instead of the current URL:

# folder/script.php?A=1&B=2  ->  xyz/1/2 (REDIRECT)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?A=([^&]+)&B=([^&\ ]+)\ 
RewriteRule ^folder/script\.php$ /xyz/%1/%2? [R=301,L]


来源:https://stackoverflow.com/questions/4054738/rewriterule-loop-redirection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!