nginx rewrite with the value of the query string only - nginx

I want to redirect from
www.setup.com/view.php?id=213
to
www.setup.com/213
this is my nginx redirect
location ^~ view.php {
if ($query_string ~ "^id=([0-9-]+)$"){
rewrite ^/view.php$ http://$server_name/%1? break;
}
}
For some reason it appends
?id= to the url
so it becomes
www.setup.com?id=213
how can I remove
?id= from the url?

Use :
location / {
rewrite ^/view.php$ /$arg_id? redirect;
}

Related

convert url get parameters to url segment in nginx

I have this URL https://example.com/user?param1=value1&param2=value2&param3=value3
and have it to go to https://example.com/user/value1/value2/value3 on the Nginx server.
Just FYI it is WordPress site and I have added the following rule in the Nginx config file.
location ~ /user/ {
if ($args ~* "^param1=(\d+)&param3=(\d+)&param3=(\d+)") {
set $param1 $1;
set $param2 $1;
set $param3 $1;
set $args '';
rewrite ^.*$ /user/$param1/$param2/$param3 permanent;
}
}
Your solution has two errors, 1) the location does not match /user, and 2) the rewrite is also appending the original arguments.
This could be fixed by using an exact match location and a trailing ? on the rewrite. For example:
location = /user {
...
rewrite ^ /user/$param1/$param2/$param3? permanent;
}
However, the map statement is a cleaner and extensible solution, for example:
map $request_uri $redirect {
default 0;
~*^/user?param1=(?<p1>\d+)&param2=(?<p2>\d+)&param3=(?<p3>\d+)$ /user/$p1/$p2/$p3;
}
server {
...
if ($redirect) { return 301 $redirect; }
...
}
See this document for details.

Removing only a specific URL parameter in Nginx with rewrite

I'm trying to make Nginx remove only a specific parameter from the requested URL. Let's say that the parameter name is par2. Here are the examples of requested URLs:
www.example.com/page?par1=a&par2=b
www.example.com/page?par2=b
Those pages should redirect to:
www.example.com/page?par1=a
www.example.com/page
Here is what I have in conf file at the moment:
location / {
...
if ($args ~* "par2") {
rewrite ^(.*)$ $request_uri permanent;
}
}
This partly works - it does remove par2 parameter, but it removes all of the other parameters as well and redirects to:
www.example.com/page
www.example.com/page
How to make it remove only a specific parameter (par2 in this case)?
EDIT:
As far as I know, it's recommended to use if only if it's followed by rewrite, so I'd prefer such a solution.
This should work for your use case when parameter can be anywhere
location /page {
if ($request_uri ~ ^(/page)(.par2=[0-9a-zA-Z]&?)$){
return 302 $1;
}
if ($request_uri ~ ^(/page\?.+)(.par2=[0-9a-zA-Z])(.*)$){
return 302 $1$3;
}
if ($request_uri ~ ^(/page\?)(par2=[0-9a-zA-Z]&?)(.+)$){
return 302 $1$3;
}
}

nginx rewrite not working - returning 404

I'm trying to rewrite :
http://example/test/ -> http://example/new/
http://example/test/check -> http://example/new/check
location ~/test/(.*)$ {
rewrite ^/new/$1?$args permanent;
}
What am I doing wrong?
Your rewrite statement is incorrect. See this document for more.
To use rewrite to capture the latter part of the URI, try:
rewrite ^/test/(.*)$ /new/$1 permanent;
Alternatively, to use location to capture the latter part of the URI, try:
location ~ ^/test/(.*)$ {
return 301 /new/$1?$args;
}

How to write this nginx rewrite rule

I have a nginx rewrite rule like this
location ~* /question\-(.*)\.html$ {
rewrite "^/question-([0-9]+).html$" "/question/$1.html";
rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;
}
This rule mean is:
if URI is /question-123456.html so rewrite to /question/123456.html
/question/123456.html is static file, and via question.php to create.
So when I visit http://example.com/question-123456.html HTTP rewrite to http://example.com/question/123456.html if not exist, I want to execute next rewrite rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;
Other than return 404 to user.
I would write it this way:
location ~ /question-(.*)\.html$ {
try_files /question/$1.html /question.php?id=$1;
}

nginx url rewrite rule

I have URL like this
http://domain/PROD_SHEP_PDF_Downloader/DownloadPdf?favorId=10100018565295&lang=ru
I want to rewrite this part PROD_SHEP_PDF_Downloader to this SHEP_PDF_Downloader so the result will be
http://domain/SHEP_PDF_Downloader/DownloadPdf?favorId=10100018565295&lang=ru
This rule doesn't work
location /PROD_SHEP_PDF_Downloader/ {
rewrite ^/PROD_SHEP_PDF_Downloader/(.*) SHEP_PDF_Downloader/$1 break;
proxy_pass http://localhost:85;
}
Docs say that if you just want to change the URI prefix, you can use this:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}

Resources