Rewrite to add to query string

I don’t understand why I always have such a serious problem with rewrite rules, but I just want to add to the query line, if it exists, and add ? if it is not. I really don't care if the URL has changed in the browser or not - it just needs to load the correct landing page.

 RewriteRule /cia16(.*)\?(.*) /cia$1?$2&CIA=16 RewriteRule /cia16(.*) /cia/$1?CIA=16 

If I go to /cia16/steps.php?page=1 , it actually /cia/steps.php?CIA=16 to /cia/steps.php?CIA=16 - this means that part of the query string is not even considered part of the URL for rewriting purposes.

What do I need to do to get rewrite to work correctly with an existing query string?

+4
source share
1 answer

You cannot match a query string in a RewriteRule , you need to match it with the variable %{QUERY_STRING} in a RewriteCond . However, if you just want to add a query string, you can simply use the QSA flag:

 RewriteRule /cia16(.*) /cia/$1?CIA=16 [QSA] 

URI: /cia16/steps.php?page=1 will be rewritten to /cia/steps.php?CIA=16&page=1 . If for some reason you need page=1 to CIA=16 , you can do something like this:

 RewriteRule /cia16(.*) /cia/$1?%{QUERY_STRING}&CIA=16 
+7
source

All Articles