Nginx rewrite a folder with exclude - nginx

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

Related

Rewrite with multiple back reference with .html file to domain name

About:
I'm trying to rewrite my domain using Nginx:
I have a website on which I'm trying to rewrite the URL to www.my.domain.com to make it secure as it's showing the whole directory structure
Right now when I write in the browser
www.my.domain.com
It redirects me to this path below
www.my.domain.com/dreamfactory/dist/index.html#/login
I want to hide the directory structure from URL to keep it safe, on search www.my.domain.com
Results should be:
www.my.domain.com/
Implemented Rewrites:
location / {
try_files $uri $uri/ /index.php?$is_args$args;
}
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^(.*[^/])$ $1/ permanent;
rewrite ^(.*)/$ $1.html break;
}
Any Hope 🤞🤞🤞🤞🤞🤞🤞🤞

Nginx - if pattern 404 - go to

I have list of urls like:
domain.com/some-url-key-with-possible-id-after-it-99999.html
I need to try that URL, and IF it returns 404, redirect to:
domain.com/some-url-key-with-possible-id-after-it.html
Is that possible?
location ~ /([a-zA-Z0-9\-]+)-([0-9]+).html$ {
## IF ABOVE IS 404
return 301 http://domain.com/$1.html;
## ENDIF
}
I found something like this:
server {
listen 12440;
root /some/path/here/nginx/html/noahc/;
server_name www.domain.net, domain.net;
port_in_redirect off;
location /{
error_page 404 = #foobar;
}
location #foobar {
rewrite .* / permanent;
}
}
But it doesn't satisfy me, because I need to redirect to url with variable from request pattern. It could be ok, if I'll be able to pass ([a-zA-Z0-9-]+)-([0-9]+) to it as an argument.
So you have a URI and you would like to rewrite it if the static file does not exist. Use try_files to test for file existence.
root /path/to/docroot;
location ~ ^(/[a-zA-Z0-9-]+)-[0-9]+\.html$ {
try_files $uri #rewrite;
}
location #rewrite {
return 301 $1.html;
}
You can use return 301 or rewrite ... last in the named location, depending on how visible you want the rewrite to be.
See this document for help with nginx directives.

NGINX location wildcard

We have multiple subfolders under public_html which basically just go
/year2014
/year2015
...
Can I do an nginx conf.d location wildcard like so and how?
location /year2014 {
rewrite ^/year2014 /(/.*)$ /app/webroot$1 break;
try_files $uri $uri/ /year2014 /app/webroot/index.php?$args;
}
Can I turn location /year2014 as a wildcard variable meaning it can be anything because all these share the same setting?
How about this (not tested)? Note the location changes to root and the double rewrite rule to cover for slash at the end.
location / {
rewrite "^/year(.*)" /app/webroot/year$1/;
rewrite "^/year(.*)/" /app/webroot/year$1/;
...

Configure nginx to rewrite hashes on all paths

I use nginx as a static file server first and try to rewrite all uri if they contain hashes to point to a mock directory.
It might be more clear with an example
let's say the user request pages url like theses :
/api/some/path/to/ecbac7cb-21ca-3d22-98ed-4d063f138d0b/some/other/path/02167621-5c01-45c5-98ba-eb3ba1bf4b2a/list
/api/some/other/path/to/ecbac7cb-21ca-3d22-98ed-4d063f138d0b
I want to try to get the file at first but the fallback would be somthing like this
/api/some/path/to/__any_hash__/some/other/path/__any_hash__/list.json
/api/some/other/path/to/__any_hash__.json
here is a config I tried to far... but obviously, it's not working.
server {
...
location /api {
try_files $uri $uri/index.html $uri.html $uri.json $uri.xml #rewrites;
}
location #rewrites {
rewrite /([0-9\-a-f]{36})/ __any_hash__ break;
try_files $uri.xml $uri.json #backend;
}
Anyone have an idea?
Your location #rewrites should probably look like this:
location #rewrites {
rewrite "^(.*)/[0-9\-a-f]{36}(/.*)$" $1/__any_hash__$2 last;
return 404;
}
The $1 and $2 capture the URI fragment before and after the hash to be substituted. The last causes the location /api to be reattempted after the substitution. The process will loop until all of the substitutions are made. The return 404 is only invoked if the fully substituted URI fails to find a file.

redirect any path but only exclude /

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

Resources