user2992672
user2992672

Reputation: 408

RewriteRule with {QUERY_STRING}

I need to make a redirect form an old page to a new one withing same webdite. The problem is that the old URL has {QUERY_STRING}.

Old http://hostelcomfort.com.ua/?page_id=12
New http://hostelcomfort.com.ua/o-gostinitse/

So far the best I could come up with was

RewriteCond %{QUERY_STRING} ^page_id=(.*)$
RewriteRule ^/?$ /o-gostinitse/ [R=301,L]

However it keeps adding the query string at the end /o-gostinitse/?page_id=12.

How can I edit the second line?

Upvotes: 0

Views: 49

Answers (1)

anubhava
anubhava

Reputation: 784998

You need to use:

RewriteCond %{QUERY_STRING} ^page_id=12$
RewriteRule ^/?$ /o-gostinitse/? [R=301,L]

? in the target URL will discard any existing query string.

PS: Starting from Apache 2.4 you can also use QSD (Query String Discard) flag:

RewriteCond %{QUERY_STRING} ^page_id=12$
RewriteRule ^/?$ /o-gostinitse/ [R=301,L,QSD]

Upvotes: 2

Related Questions