Nginx rewrite with query - nginx

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;
}

Related

How can I use nginx to set a vhost and webroot on a URI of an existing vhost?

I want to send a vhost's requests to git.domain1.tld to sub.domain2.tld/git
I suppose that would conflict with overwritten files so how can I get that domain to point to that location?
You can try this:
location / {
proxy_pass https://sub.domain2.tld/git;
}
Or
rewrite ^/$ https://sub.domain2.tld/git permanent;
rewrite ^/(.*)$ https://sub.domain2.tld/git/$1 permanent;

Nginx rewrite with basic regex

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;
}

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;
}

Nginx rewrite domain and URLs

I'm configuring nginx with multiple server names, and trying to set up the following rewrite rules
redirect / on old.domain.com to new.domain.com/specific_page.php
redirect old.domain.com/$1 to new.domain.com/$1
In my "server" configuration, I have already the first rewrite condition working, but I cannot find the way to write the second.
if ($host = 'old.domain.com' ) {
rewrite ^/ http://new.domain.com/my-specific/link/list/info.php permanent;
rewrite ^/(.*)$ http://old.domain.com/$request_uri? permanent;
}
Any ideas how to handle easily this scenario? (I realise this might be an unusual setup.)
Actually I managed to solve my problem :
if ($host = 'old.domain.com' ) {
rewrite ^/$ http://new.domain.com/my-specific/link/list/info permanent;
rewrite ^(.*)$ http://old.domain.com$request_uri? permanent;
}
the first rewrite rule ^/$ matches only http://old.domain.com/ and rewrites it to the requested URL
The second rewrite rule ^(.*)$ matches whatever is behind the http://old.domain.com/ and rewrites the domain only.

Resources