Madoc Comadrin
Madoc Comadrin

Reputation: 568

Apache2: Rewrite url containing question mark

I am using Apache 2.4.7. I use mod_rewrite to alter some urls.

I want to rewrite http://example.com/servicename/oldpage?id=abcto http://example.com/servicename/newpage.

Other similar rewrites work so I belive the ? inside url is causing problems. I have tried escaping it with \.

This works as there is no ? in url:

RewriteRule ^/servicename/old /servicename/new

But these don't work:

RewriteRule ^/servicename/oldpage?id=abc /servicename/newpage

RewriteRule ^/servicename/oldpage\?id=abc /servicename/newpage

I have also tried using RewriteCond from examples like this: .htaccess rewrite URL with a question mark "?" but I didn't manage to get them work.

How should rewrite url that contains question mark?

EDIT: I tried solutions given in Match Question Mark in mod_rewrite rule regex but was not able to make them work for me. That question is about preserving query string when rewrite while I want to remove it when rewriting.

Upvotes: 1

Views: 806

Answers (1)

Dusan Bajic
Dusan Bajic

Reputation: 10869

RewriteRule pattern is matched against the part of the URL after the hostname and port, and before the query string.

When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded.

So, this should work:

RewriteRule ^/servicename/oldpage /servicename/newpage [QSD]

Upvotes: 4

Related Questions