If I have URL like this -


and want it to be rewritten to this


Since magento2 URL rewrite won't take query how can this be achieved with apache and nginx rewrites?

  • url_key for the category? Commented Feb 21, 2019 at 10:08
  • url_key for category is products and page is loaded by given collection id which in this case is 99. so mainpage.com/products is given this query ?test_category=99 to load collection on this url mainpage.com/products?test_category=99 which needs to be rewriten
    – Trenox
    Commented Feb 21, 2019 at 10:17
  • rewriteing mainpage.com/products?test_category=99 to mainpage.com/testproducts is easy in Apache or Nginx, what are other cases? Are there other parameters than test_category? What should happen with the values? Can you generalize the requirements for the rule you need?
    – HelgeB
    Commented Feb 21, 2019 at 10:34
  • There are many pages whose collections are generated like this. But rewrite is only needed for this specific url with param 99
    – Trenox
    Commented Feb 21, 2019 at 14:20

2 Answers 2


For apache you can use the following - for example just put it into your .htaccess file in the document root directory:

RewriteCond %{REQUEST_URI} ^/products.*$
RewriteCond %{QUERY_STRING} ^.*test_category=99.*$
RewriteRule .* /testproducts? [R=301,NC,L]

This does a 301 redirect. If you just want to serve the content (i.e. internal rediect so the client still sees the original URL) please remove R=301 from the rule's option list.

The question mark at the end of the rewrite url means that the query string will entirely be removed (even if there are other parameter except the test_category)

Update: For the updated requirement that /products?test_category=99 should redirect to /testproducts and this page should server the content from /products?test_category=99. In order to avoid endless redirect loops, we need to append a parameter for the internal redirect which helps us to skip the first redirect rule. If the parameter redirected=1 is a problem for the /products URL choose one which fits.

#Redirect /products?test_category=99 to /testproducts
RewriteCond %{REQUEST_URI} ^/products.*$
RewriteCond %{QUERY_STRING} ^.*test_category=99.*$
RewriteCond %{QUERY_STRING} !^test_category=99&redirected=1$
RewriteRule .* /testproducts? [R=301,NC,L]

#Serve content from /products?test_category=99 for /testproducts
RewriteCond %{REQUEST_URI} ^/testproducts$
RewriteRule .* /products?test_category=99&redirected=1 [NC,L]

Update for URLs that have to pass the Magento routing: In this case the REQUEST_URI is used by Magento and this will not be overwritten by the last rule in the example above. A solution in this case is to use the P-flag for the RewriteRule. In this case the Apacehe mod_proxy module needs to be installed!

RewriteRule .* /products?test_category=99&redirected=1 [NC,P]

Update with a better variant using Magento's rewrite routing for the second rewrite /testproducts to /products?test_category=99: Just add a url rewrite with request_path = 'testproducts' and target_path = 'products/test_category/99' and in Apache's .htaccess file only the first RewriteRule is needed.

I hope that helps.

  • unfortunately this did nothing
    – Trenox
    Commented Feb 21, 2019 at 15:02
  • Sorry I've copied the wrong one first time in my answer, I have updated it, the query_string needs to be separately
    – HelgeB
    Commented Feb 21, 2019 at 17:01
  • will try this tommorow and update if it works. Cheers
    – Trenox
    Commented Feb 21, 2019 at 17:06
  • Now if going to this mainpage.com/products?test_category=99 Url rewrites to mainpage.com/testproducts, but the content of first url is not loaded on the second url, so I'm prompted with 404. And When going directly to mainpage.com/testproducts 404 aswell.
    – Trenox
    Commented Feb 22, 2019 at 6:41
  • So you want to redirect from mainpage.com/products?test_category=99 to mainpage.com/testproducts and mainpage.com/testproducts should serve the content from mainpage.com/products?test_category=99? Is that right? If yes it will become a little more complex :-)
    – HelgeB
    Commented Feb 22, 2019 at 7:23
location / {
    if ($arg_test_category = 99 ) {
        return 301 https://mainpage.com/testproducts;
    try_files $uri $uri/ /index.php$is_args$args;
  • you dont need if to do a simple rewrite
    – MagenX
    Commented Mar 3, 2019 at 11:53
  • @MagenX how will you approach redirect for URL with specific parameter then? Commented Mar 3, 2019 at 11:54
  • @MagenX the question is about redirecting based on specific URL argument in original URL. The rewrite directive has no idea about those. It can only either discard or append existing parameters, not take decision to redirect based on the value of an argument Commented Mar 3, 2019 at 12:01
  • maybe using map+rewrite to load multiple regex'ed variants. just loking for options
    – MagenX
    Commented Mar 3, 2019 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.