1
votes

I want to do a mod_rewrite URL transformation. I want to direct

http://localhost/events?user=XXX&start=YYY&total=ZZZ

to

http://localhost/?operation=events&user=XXX&start=YYY&total=ZZZ

What I am using currently in my .htaccess is:

RewriteRule
    ^events\?userid=([0-9]+)&start=([0-9]+)&total=([0-9]+)$
    ?operation=events&user=$1&start=$2&total=$3
    [L]

but it does not seem to be working.

I have also tried a simpler version:

RewriteRule ^events\?([a-zA-Z0-9&=]+)$ ?operation=events&$1 [L]

But it is also not working.

¿How can I "transfer" either the query params one by one OR else the whole query string to the end of the directed path?

1

1 Answers

2
votes

How can I "transfer" either the query params one by one OR else the whole query string to the end of the directed path?

This is done using QSA flag. QSA (Query String Append) flag preserves existing query parameters while adding a new one.

However remember that you cannot match query string using RewriteRule directive. You can only match Request URI using RewriteRule.

You can use this rule:

RewriteEngine On

RewriteRule ^/?events/?$ ?operation=userevents [L,NC,QSA]

This will route http://localhost/events URI to http://localhost/?operation=userevents by appending all the original query parameters to rewritten URI. For example: http://localhost/userevents?user=XXX&start=YYY&total=ZZZ will be rewritten to http://localhost/?operation=events&user=XXX&start=YYY&total=ZZZ