1

I am setting up Query string redirect :

expo.com/en/general/campaigns/on-second-thought.html?slide=ost-2016-tank to expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html

RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} slide=ost-2016-tank
RewriteRule  ^/en/general/campaigns/on-second-thought.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC] 

redirect happening but its appending ?slide=ost-2016-tank like below

http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html?slide=ost-2016-tank 

slide=ost-2016-tank parameter is added to redirected page

hjpotter92
  • 71,576
  • 32
  • 131
  • 164
Kalyan K
  • 11
  • 2

2 Answers2

1

Since your rule does not define a new query string, the default behavior of Apache is to copy the old query string to the new URL. To get rid of it, append a ? to the address you rewrite/redirect to:

 RewriteRule  ^/en/general/campaigns/on-second-thought\.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html? [R=301,L,NC]

Or, for Apache >= 2.4, you can also use the flag QSD (Query String Discard):

RewriteRule  ^/en/general/campaigns/on-second-thought\.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC,QSD]
julp
  • 3,333
  • 1
  • 18
  • 19
1

Simply add a blank query string when redirecting:

RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} ^slide=(ost-2016-tank)$
RewriteRule  ^(/?en/general/campaigns/on-second-thought)\.(html)$ $1/%1.$2? [R=301,L,NC]

No need to mention http://expo.com again when redirecting. It'll automatically redirect to the same hostname because of R flag. No need to repeat same strings over and over. Using match groups and referencing them later works.

Your pattern had .html?$ in it, which actually means that it'll match .html as well as .htm. You do not receive query strings in RewriteRule context.

hjpotter92
  • 71,576
  • 32
  • 131
  • 164