John Davidson

php - Rewrite get parameters url with nginx and redirect


I've been trying for a hours without figure it out.. if someone can help me..

I have a url like this:

I want to rewrite a url to:

I have tried:
rewrite ^/?test/(.*?)/?$ /test.php?action=$1 last;

however i need to url to redirect too for the new url, also i don't know how to rewrite with multiple params/values..

Thank you in advance!

My Answer:
To rewrite the URL with Nginx and redirect, you can use the following configuration in your Nginx server block:

server {
listen 80;

location /test.php {
if ($args ~* "action=value1") {
rewrite ^/test.php /new-url? permanent;

location /new-url {
# Handle the request for the new URL here

In this configuration, Nginx will check if the query parameter `action` has the value `value1` in the URL `/test.php`. If it does, Nginx will rewrite the URL to `/new-url` and redirect the request with a 301 status code.

Make sure to replace `/new-url` with the actual URL you want to redirect to and add the necessary configuration to handle the request for the new URL in the corresponding `location` block.

After making these changes, don't forget to reload Nginx for the changes to take effect:

sudo systemctl reload nginx

This should help you rewrite the get parameters URL with Nginx and redirect it to a new URL.

Rate this post

5 of 5 based on 7662 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us