Search code examples
regex.htaccesshttp-redirectmod-rewriteurl-rewriting

Rewrite query string with another get params


I have a link like this:

/catalog/section/?PAGEN_1=1&search=someWord

/news/?PAGEN_1=2

where PAGEN_1=(page number)

I try to set URL to:

/catalog/section/?page=1&search=someWord

/news/?page=2

But this not work for me. What am I doing wrong?

RewriteCond %{QUERY_STRING} (?:^|&)PAGEN_1=(.*)$
RewriteRule ^/(.*)$ /$1?page=%1 [L,R]

I need to save another GET params.


Solution

  • You can use this rule to rename a query parameter from PAGEN_1 to page:

    RewriteCond %{QUERY_STRING} ^(.*&)?PAGEN_1=([^&]*)(&.*)?$ [NC]
    RewriteRule ^ %{REQUEST_URI}?%1page=%2%3 [R=302,L,NE]
    

    Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.