1

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.

Community
  • 1
  • 1
Madoc Comadrin
  • 498
  • 1
  • 12
  • 27

1 Answers1

3

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]
Dusan Bajic
  • 10,249
  • 3
  • 33
  • 43