Nginx rewrite with basic regex - nginx

Trying to get
http://example.com/shop/donate?id=123
to redirect to
http://example.com/donate/123
With the below in my .conf, hitting the top URL does not result in a redirect. Other redirects in the .conf are working. Any thoughts?
server{
....
rewrite ^/shop/donate?id=([0-9]+)$ /donate/$1 permanent;
}

You can try use $arg_[name] for this case:
rewrite ^/shop/donate$ /donate/$arg_id;

You need to escape the question mark, else it gets parsed as regex:
server{
....
rewrite ^/shop/donate\?id=([0-9]+)$ /donate/$1 permanent;
}

Related

Redirecting old url to Nginx with param

I have an old URL structure like this archives.php?l=e and I moved it to a new structure archives/e
I'm trying to 301 redirect all the old urls, but I have run into troubles. This is what I'm currently doing but it's not working.
location ^~ /list.php {
rewrite /archives/$1/ permanent;
}
Any help would be greatly appreciated.
I figured it out and I have to say Nginx has some nice magic for this. Using arg_l solved it. Somehow it creates a variable for each param.
location ^~ /archives.php {
rewrite ^/archives.php$ /archives/$arg_l? permanent;
}

Chage the part of the URL using nginx

I m using nginx webserver.
I want to change the url before it hits the server from
https://www.example.com/abc/contact-us
to
https://www.example.com/#/contact-us
Thanks in advance.
For a single URI redirection, an exact match location and return statement may be most efficient:
location = /abc/contact-us {
return 301 /#/contact-us;
}
To redirect all URIs beginning with /abc use a rewrite directive:
location ^~ /abc/ {
rewrite ^/abc(.*)$ /#$1 permanent;
}
The location block is largely redundant, but means nginx only looks at the regular expression when it needs to. See this document for more.

Nginx rewrite with query

I need to convert this 301 redirect to a nginx rewrite
Redirect 301 /details.php?floorID=123 http://www.example.com/flooring.html
I tried this and it works:
rewrite ^/details.php /flooring.html permanent;
but when I add the variables it does not recognise anything after the ?
This is what I need, but it does not work:
rewrite ^/details.php?floorID=123 /flooring.html permanent;
Can anyone advise?
Thanks
This works:
if ($query_string ~ "^floorID=123"){
rewrite ^/?details.php$ /flooring.html? permanent;
}

Vanity url redirection from nginx with # in url

I want to redirect mysite.com/#parag to mysite.com/#/conversation/parag
Below is the nginx configuration I have written but not working. What could I be doing wrong.
location ~ /\# {
rewrite ^/\#(.*)$ /#/conversation/$1 break;
}
Thanks in advance.
If there is another location to capture uri /#/conversation/, use
rewrite ^/\#(.*)$ /#/conversation/$1 last;
instead of
rewrite ^/\#(.*)$ /#/conversation/$1 break;
See here for rewrite flags.

Redirect a specific URL to another in NGINX

I need to redirect a specific url containing a parameter and an anchor to a new URL:
/pages/page.php3?page=fond_razeni_gk.htm#GKI
to
http://www.newserver.com/o-knihovne
I try:
rewrite ^/pages/page.php3\?page=fond_razeni_gk.htm$ http://www.newserver.com/o-knihovne? redirect;
but this does not work. Could you please advise me what is wrong?
try this configuration:
location / {
rewrite ^/pages/page.php3(.*)$ http://www.newserver.com/o-knihovne$1 permanent;
}

Resources