adriaan
adriaan

Reputation: 1118

Remove query string after rewrite old URLs with mod_rewrite

For my Dutch website I want to redirect some old URLs to a new path.

I want to redirect URLs like this:

/playlist?artist=Bob+Marley    ->    /zoeken/Bob+Marley
/playlist?artist=Rammstein     ->    /zoeken/Rammstein

I'm come up with this to far:

RewriteCond %{QUERY_STRING}  ^artist=(.*)$  [NC]
RewriteRule ^playlist        /zoeken/%1     [NC,L,R=301]

The only problem right now is that the query string keeps showing up in after the redirect:

http://watiseropderadio/zoeken/Bob+Marley?artist=Bob+Marley

How do I remove this query string?

Upvotes: 0

Views: 4432

Answers (1)

Jon Lin
Jon Lin

Reputation: 143926

You need a ? at the end:

RewriteCond %{QUERY_STRING}  ^artist=(.*)$  [NC]
RewriteRule ^playlist        /zoeken/%1?     [NC,L,R=301]
# here --------------------------------^

By default, query strings are appended to the rule's target automatically unless you have a ? to construct your own query string. With just a ? at the end, you're constructing a blank query string and the existing query string won't get appended without the QSA flag.

Upvotes: 2

Related Questions