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;
}
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;
}
}
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;
}
I would like to rewrite the following URL http://example.net/bar/foo to http://example.net/bar.
Do you know how to do this ?
Use this :
location /bar {
rewrite ^/bar/(.*)/[^/]+$ /bar/$1;
}
I need some help to understand the Nginx rewrite function.
Imagine, a real file is at the following address:
http://www.domain.com/library/content/folder/country/page/index.html
I want to rewrite the URL, to have something better (more human readable!):
http://www.domain.com/page
1. How can I do that with Nginx?
location = /... {
rewrite ...
}
Or
if ($request ~* "page") {
rewrite ...
}
2. How can I write this rule ?
If I was using Apache, I will write something like this:
RewriteRule ^page /(.*) library/contents/folder/contry/page/$1 [L]
You can use the alias or root instructions in a location block
location /page/ {
root /my/absolute/path/library/content/folder/country/;
}
location /page/ {
alias /my/absolute/path/library/content/folder/country/page/;
}
Source: http://wiki.nginx.org/HttpCoreModule
How about
This problem is related to versioning of API based on header
INPUT url : http://abcd.com/v1/getshafts?param=q ,header = v1.1
OUTPUT : abcd.com/v1.1/getshafts/
what should be 'rewrite' value here?