I have this URL https://example.com/user?param1=value1¶m2=value2¶m3=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+)¶m3=(\d+)¶m3=(\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+)¶m2=(?<p2>\d+)¶m3=(?<p3>\d+)$ /user/$p1/$p2/$p3;
}
server {
...
if ($redirect) { return 301 $redirect; }
...
}
See this document for details.
Related
I would like to rewrite legacy links using a query parameter type of URL to a new style of URL.
Ex.
example.com/page?id=1 -> example.com/page/1
example.com/otherpage?id=1 -> example.com/otherpage/1
Currently I have the following configuration using the evil if.
if ($args ~* "id=(.*)") {
set $w1 $1;
rewrite .* $scheme://$host/page/$w1? permanent;
}
Note: I am using CloudFront, and relying on the host header above.
If the above is in a server block, with no other location block - would this qualify as a non-evil use of if in NGINX config? Also, the above only supported /page/. Any better ideas for making that portion work for otherpage and other pages?
I have seen a few other ideas discussing using a map, but I'm not quite sure how to bring it all together? I was thinking something along the lines of:
map $args_id ?? {
default ?
??
}
...
server {
...
???
}
UPDATE:
Based on the Answer from #Ivan, this was my final solution:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
# Handle legacy requests
if ($args ~* "id=(.*)") {
set $w1 $1;
rewrite ^ $scheme://$host$uri/$w1? permanent;
}
}
Your if construction isn't evil. You can use something like
rewrite ^ $scheme://$host$uri/$w1? permanent;
for any page. More complex example if you want to process both example.com/page?id=1 and example.com/page/?id=1:
map $uri $maybe_slash {
~/$ "";
default "/";
}
...
server {
...
rewrite ^ $scheme://$host$uri$maybe_slash$w1? permanent;
...
}
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;
}
}
My URL looks like:
localhost/video-detail?videoID=T0r-uCXvDzQ
I want to serve a page with name: T0r-uCXvDzQ.html (videoID.html) which is present in server's file system.
I am trying to write the rewrite rule as follows:-
location / {
rewrite ^/video-detail?videoID=(.*) /$1.html;
}
Also tried:
location / {
rewrite ^/video-detail?videoID=(.*) /$arg_videoID.html;
}
But they are giving 404 error.
How can I use the query parameters in the output rewrite rule.
The following worked for me:-
if ($args ~* "videoID=(.*)") {
set $key1 $1;
rewrite ^(/video-detail)$ /$key1.html;
}
I have queries like /api/lang?lang=en which I want to serve with nginx as /server/i18n-angular/en.json. How can I do that?
I have the following directory structure:
/public/
/server/i18n-angular/en.json
I have the following configuration, but nginx says it is wrong to use index directive at that point.
server {
root /public
...
location /api/lang {
if ($args ~* "\?lang=(.+)") {
set $language $1;
index ../server/i18n-angular/$language.json;
}
}
}
What directive should I use instead of index?
location /api/lang {
alias /server/i18n-angular;
rewrite ^ /$arg_lang.json;
}
"index" specifies index file for displaying a folder, so it's not what you need.
You require "rewrite" instead:
location /api/lang {
alias /server/i18n-angular;
if ($args ~* "\?lang=(.+)") {
set $language $1;
rewrite ^/(.*)$ /$language.json;
}
}
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;
}