Nginx rewrite domain and URLs - nginx

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.

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 is encoding querystring as a path

I want to rewrite internally several locations /customers/foo?bar=2 to an existing location in my nginx configuration at /blah so that it's as if the request was to /blah/customers/foo?bar=2.
location /blah {
# View in fiddler
proxy_pass http://127.0.0.1:8888/;
# Lots of config here I don't want to repeat everywhere else
}
location /customers/ {
rewrite ^/customers/(.*) /blah/customers/$1$is_args$args;
}
location /other/ {
rewrite ^/other/(.*) /blah/other/$1$is_args$args;
}
# etc...
Nginx is rewriting the URL with the querystring encoded as a path /blah/customers/foo%34bar=2.
The same thing happens with rewrite ^ /blah$request_uri;. It encodes the ? as %3F effectively garbling the URL.
If I do a client redirect rewrite ^ /blah$request_uri permanent; the URL is correct and contains the ? but I want an internal redirect inside my NGINX config.
Don't use $is_args$args, because the rewrite directive will automatically append any existing query string.
For example:
rewrite ^/customers/(.*) /blah/customers/$1 last;
Although, I would prefer:
rewrite ^(.*)$ /blah$1 last;
Or even:
rewrite ^ /blah$uri last;

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

Nginx rewrite rules additional arguments

I'm breaking my head on Nginx rewrite rules while migrating from Apache to Nginx.
I had .htaccess rewrite rules that made /search/foo+bar/2&pricerange=20-50 to search.php?search=foo+bar&page=2&pricerange=20-50
and I could access all the arguments like you would expect with $_GET['search'], $_GET['page'] and $_GET['pricerange']
But now with Nginx I have issues with this appended arguments like pricerange. Whenever I visit /search/foo+bar/2&pricerange=20-50 it does not translate the pricerange argument.
And when I visit the same url without the page number the pricerange argument gets added to the search argument. But only when I'm using more than one word concatenated with +'s.
My current Nginx rewrite rules:
location / {
try_files $uri /index.php$is_args$args;
rewrite ^/search/(.*)/(.*)/$ /search?search=$1&page=$2 last;
rewrite ^/search/(.*)/(.*)/?$ /search?search=$1&page=$2 last;
rewrite ^/search/(.*)/$ /search?search=$1 last;
rewrite ^/search/(.*)/?$ /search?search=$1 last;
}
GET arguments begin after the '?'
/search/foo+bar/2&pricerange=20-50
You need to replace '&' with '?' or rewrite rule
rewrite ^/search/(.*)/([0-9]+)&pricerange=(.*)$ /search?search=$1&page=$2&pricerange=$3 last;

nginx location fix: Redirect to index.html

I'm runnning nginx v 1.0.4 and we're trying to do the following:
location ~ ^/$ {
rewrite  ^.*$  /index.html  last;
}
Basically: If the user gets to the the default domain http://www.foo.com or http://www.foo.com/ redirect them to http://www.foo.com/index.html
When I add this to my conf file, I get the following:
Starting nginx: nginx: [emerg] unknown directive " " in /etc/nginx/myconf.conf
Thanks in advance.
You can just use rewrite function without location
rewrite ^/$ /index.html last;
or for permanent redirect
rewrite ^/$ /index.html permanent;
to rewrite with parameters, e.g. http://www.foo.com/?param=value -> http://www.foo.com/index.html?param=value
rewrite ^/(\?.*)?$ /index.html$1 permanent;

Resources