Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use RewriteCond %{QUERY_STRING} without attaching the query string to the new address

I try to do a 301 redirect with .htaccess.

The issue:

/?view=products&id=12345 -> /8831

there is no relation between the old and the new address.

For some reason

Redirect 301 /?view=products&id=12345 /8831

doesn't work. If I remove the question mark, it works without question mark.

i tried also:

RewriteCond %{QUERY_STRING} view=products&id=12345
RewriteRule .*$ /8831 [L,R=301]

but it redirects me to /8831?view=products&id=12345, which is not good for me. I don't need the query string in the new url-

like image 680
Idan Gozlan Avatar asked Mar 07 '13 16:03

Idan Gozlan


1 Answers

RewriteCond %{QUERY_STRING} view=products&id=12345 
RewriteRule .*$ /8831? [L,R=301]

The ending ? will prevent the original query parameters from being appended, unless you also give the [QSA] flag again.

From the manual:

Note: Query String The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.

like image 158
Wrikken Avatar answered Oct 03 '22 23:10

Wrikken