Luistar15
Luistar15

Reputation: 1719

RewriteRule - loop redirection

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]

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]

Upvotes: 0

Views: 446

Answers (1)

Gumbo
Gumbo

Reputation: 655129

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]

Upvotes: 1

Related Questions