Report this

What is the reason for this report?

How can I set rewrite rule for my dynamic query string api in nginx vhost

Posted on March 14, 2022


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

To set up a rewrite rule that converts your dynamic URLs, you can make use of the RewriteRule directive in an .htaccess file or directly inside your server block configuration. Here’s a general example of how it might look for your use case:

  1. RewriteEngine On
  2. RewriteCond %{QUERY_STRING} ^cc=([^&]*)&user_id=([^&]*)¤tpage=([^&]*)&trim_video_id=([^&]*)$
  3. RewriteRule ^v2/product_shots\.php$ https://dc.domain.tv/v2/product-shots-all?cc=%1&user_id=%2¤tpage=%3&trim_video_id=%4 [R=301,L]

With the RewriteCond directive, we are matching the query string parameters and with RewriteRule, we’re redirecting the request to the new URL, preserving the query string values.

This is just sample code and might need adjustments according to your exact requirement.

Please make sure to replace dc.domain.tv and abc.domain.com with your domain names and change query string parameters according to your page.

Information about the mod_rewrite module and how to use it can be found in the DigitalOcean documentation:

https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Dark mode is coming soon.