redirect any path but only exclude / - nginx

I will be changing my domain for another, only want to serve "/" in the old domain, but other paths will be redirected to the new domain following the url context.
location ^/(.*)$ {
# only serve /, other paths will be redirected to the new domain
rewrite ^/(.*)$ https://new.com/$1 permanent;
}
location / {
# only serve a html in old.com/ to explain the domain change
index index.html;
}
this is redirecting every request, including root context, what I am missing?

You missed the part that index makes internal redirect and you always end up in first location.
There is more elegant and nginx-y way to achieve your goal:
location = / {
try_files /index.html =404;
}
location / {
return 301 https://new.com$request_uri;
}

Related

Nginx Rewrite Encoded URI (with percent) Not Working

I have a WordPress site that needed to 301 redirect old post to new post.
Old post:
https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/
New Post:
https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/
I added this rule in nginx.conf for this domain here
server
{
listen 111.222.333.444:80;
server_name example.com www.example.com ;
return 301 https://www.example.com$request_uri;
}
server
{
rewrite_log on;
rewrite ^/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/$ https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/ permanent;
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
(the rest of location blocks continue)
}
Restart Nginx.
However, the old URL still return 404 and not a 301.
https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/
And I don't see neither old nor new URI in error log at all. What should I do? Thanks!
The percent encoded URL is available in the $request_uri variable. But by the time Nginx is processing rewrite and location statements, the URL has been decoded and normalised.
Use a rewrite or location statement with the decoded values. For example:
rewrite ^/สวัสดี/$ /ลาก่อน/ permanent;
Or:
location = /สวัสดี/ {
return 301 /ลาก่อน/;
}

How to use my index.htm in this location script?

I need URL http://myexample.org (root) redirecting to my local index.htm, not rewriting it to Github... How to do it?
I was testing many variations of location = / { try_files ...} but no one works. Using a UBUNTU 16 LTS server.
/etc/nginx/sites-available/myexample.org
server {
server_name myexample.org;
root /var/www/myexample.org;
index index.html index.htm;
# ?? location =/ {...} is not working!
location / {
# also not work a root rewrite
# rewrite ^/?$ index.htm break;
rewrite ^/?git$
http://github.com/myexample-org/test
break;
rewrite ^/?tickets$
http://github.com/myexample-org/test/issues
break;
rewrite ^/?(.+)$
http://github.com/myexample-org/test/$1
break;
}
}
Change your last rewrite directive to match ^/(.+)$
location = / will not work for an index, as the rewritten URI will match location / which will then hit your final rewrite statement.
Your original solution (a few questions ago) with the named location, should work fine:
root /path/to/file;
index index.htm;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/... http://example.com/...;
rewrite ^/... http://example.com/...;
rewrite ^/... http://example.com/...;
}
Assuming that a file called /path/to/file/index.htm exists on this server. The break flag is unnecessary as the destination URL begins with http://. If you want to add a flag, the redirect or permanent flag would be pertinent. See this document for details.

Rewrite directory as parameter

I'm trying to redirect requests from
example.com/abc234 to
example.com/setup.html?s=abc234
So far, I've tried the following, but it seems to always end up either 1) not transmitting the parameter or 2) ending up in an infinite loop (or 404) because it also tries to redirect the redirected request? The request has to be visibly rewritten because I want to pick up the parameter with JS, not PHP.
server {
server_name example.com;
root /var/www/html;
rewrite ^(.*)$ /setup.html?s=$1 redirect;
}
I've also tried various combinations of location / { try_files ...; } or using the absolute URL within rewrite without success.
One technique is to rewrite only URIs that do not match a physical file.
For example:
server {
server_name example.com;
root /var/www/html;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
return 302 /setup.html?s=$uri;
}
}
See this document for more.
rewrite ^(.*)$ /setup.html?s=$1 redirect;
}

How to redirect the root domain alone in nginx to another URL

I need to redirect the root path alone to a specific URL and other path to another URL.
For Example:
http://localhost/example should be redirected to http://localhost/example/index.php?route=common/home
http://localhost/example/welcome-to-this should be redirected to http://localhost/example/index.php?route=welcome-to-this
I tried this way
location /example {
rewrite ^ /example/index.php?_route_=common/home last;
}
location /example/(.*)$ {
rewrite ^/(.+)$ /example/index.php?_route_=$1 last;
}
Try this:
location = /example {
return 301 $scheme://$host/example/index.php?route=common/home;
}
location ~ ^/example/(.*)$ {
return 301 $scheme://$host/example/index.php?route=$1;
}

Nginx rewrite a folder with exclude

i am working on nginx webserver.
I want to redirect all urls inside folder1 www.site.com/folder1/ but not the subfolder1 www.site.com/folder1/subfolder1
I created these rules to nginx configuration but no luck.
location = /folder/subfolder {
}
location /folder {
rewrite ^/folder(.*) www.redirect.com permanent;
}
Am i missing something?
Ok so here's a refined answer including some of the comments I've read plus one of mine, to be able to access the assets inside the subfolder I added the try_files, and the 301 redirect in all other urls was added for the redirection.
location /folder/subfolder {
try_files $uri $uri/ =404;
}
location /folder {
return 301 $scheme://example.com;
}
Your new set of rules should be as follows. I am assuming that valid file hits are okay (i.e. the user knew the file). If you do not want this behaviour, replace try_files with the content of the #rw block:
location /folder {
try_files $uri $uri/ #rw;
}
location #rw {
rewrite ^/folder/([^\/]*) http://www.redirect.com/ permanent;
}
These should work.
Remove the "=" because that's for "exact" match. So it only matches the folder itself, and a request for "/folder/subfolder/a_file.html" won't match that block. Also you need to add $scheme in your rewrite rule. And if you just want to redirect to the home page (http://www.redirect.com), you can remove the "$1" part.
location /folder/subfolder {
}
location /folder {
rewrite ^/folder(.*)$ $scheme://www.redirect.com$1 permanent;
}

Resources