mikejohnvino mikejohnvino - 9 months ago 36
PHP Question

301 redirect not working properly with htaccess


Above url should redirect to

But now its redirecting to below url like...

I'm using this below code, but not working properly...

RewriteCond %{THE_REQUEST} /thanks\.php\?orderid=([^&\s]+) [NC]
RewriteRule ^ /order-confirmation/%1 [NC,R=301,L]
RewriteRule ^order-confirmation/([^/]+)$ /thanks\.php\?orderid=$1 [NC,L]

anyone help me, how do I do this?


You need to use ? in target URL to strip off previous query string:

RewriteCond %{THE_REQUEST} /thanks\.php\?orderid=([^&\s]+) [NC]
RewriteRule ^ /order-confirmation/%1? [R=301,L]

RewriteRule ^order-confirmation/([^/]+)$ /thanks\.php\?orderid=$1 [NC,L,QSA]

Starting with Apache 2.4 you can also use QSD (Query String Discard) flag to trip off previous query string.