John Davidson

php - Rewrite get parameters url with nginx and redirect

0 comments
Message:


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


I have a url like this:
example.com/test.php?action=value1&step=value2


I want to rewrite a url to:
example.com/test/value1/value2


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:

nginx
server {
listen 80;
server_name example.com;

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:

bash
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

Comments




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