Let's say I have example.com and I'd like to rewrite anything after / to root, i.e. example.com/one/two?whatever=foo should go back to example.com
I've tried the following:
location = / {
index index.html;
}
location / {
rewrite ^ / permanent;
}
But this gives me too many redirects error. I could go route using regex to specify all the allowed/disallowed characters, but that would make it too ugly/long/complex.
Why does the exact match can't tell the difference?
You use the index directive to perform an internal rewrite from / to /index.html. See this document for details.
nginx then restarts the search for a matching location, which results in a loop.
You could add an exact match for the /index.html URI, for example:
location = /index.html { }
If index.html pulls in local resources (e.g. css, js, images), you will also need to handle those URIs specially.
Related
Using nginx, I would like to redirect all my static html URLs( mydomain.com/index.html; mydomain.com/contact.html; mydomain.com/about.html ) to the same urls without extension but with a slash at the end.
The end urls should be like that:
mydomain.com/index/
mydomain.com/contact/
mydomain.com/about/
....
How can I achieve that? Thanks.
There are two parts to this question. The first is how to make the server return contact.html in response to the URI /contact/.
There are a number of ways to achieve this. For example:
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^(/.+)/$ $1.html last;
return 404;
}
Any URI ending with a / will be internally mapped to the same URI with the / replaced by .html.
The second part is if people are still accessing the .html URIs using the old scheme, you may want to externally redirect them to the new scheme.
The best way to achieve this without creating a redirection loop, is to check the original request stored in $request_uri. For example:
if ($request_uri ~* "\.html(?|$)") {
rewrite ^(.*)\.html $1/ permanent;
}
i currently have a problem configuring my Nginx correctly for nuxt.js generated sites.
What i want to achieve is the following
/magazin -> /magazin/index.html
/magazin/ -> 301 /magazin
/magazin/artikel/titel-goes-here -> /magazin/artikel/titel-goes-here/index.html
/magazin/artikel/titel-goes-here/ -> 301 /magazin/artikel/titel-goes-here
currently this is the other way around.
If im correct i shouldn't use a proxy pass to a e.g. pm2 instance with express etc. as it destroys the sense of static site generation.
But how can i get this page structure to work, as i need the same url's as our legacy service for SEO reasons, which used Angular Universal SSR
my current config is:
location ^~ /magazin {
root /path/to/dist;
index index.html ;
}
if i add something like
rewrite ^(.+)/+$ $1 permanent;
i get an infinite 301 loop
Thanks for the help
You cannot use the built in index directive, as it works the other way around (as you have observed).
You can use try_files to test for the existence of the index.html file. Use a named location to process the redirection.
For example:
location ^~ /magazin {
root /path/to/dist;
try_files $uri $uri/index.html #rewrite;
}
location #rewrite {
rewrite ^(.+)/$ $1 permanent;
}
See this document for details.
Url example:
http://test.com/test/page/4?-test?o?o_html/page/100/page/2/page/3/page/4/page/4/page/2/page/106/page/107/page/2/page/3/page/4/page/108/page/3/page/2/page/3/page/4&-test
I want to use nginx location to forbidden it.
But I faild, I have tried different rules in http://nginx.viraptor.info/
location ~ /page/.*/page/ {
return 403;
}
location ~* \/page/.*/page/ {
location ~* /page/\.*/page/ {
None of them worked...
I found only use /page/ is Worked.
location ~* /page/ {
But when I add .*/page/ like:
location ~* /page/.*/page/ {
It's not worked...
Now I use php to judge url like:
if (preg_match ("/\/page\/.*\/page\//i", $_SERVER["REQUEST_URI"]))
Please tell me how to use regex .* in nginx conf location. I want to use nginx.
Everything after the first ? are URI arguments, they are not part of the URI, as such a location match will never work. If there is a solution that allows you to do this in Nginx, it will be a kludge, you should perform this level of checking in your script like you stated you are already doing.
See: https://serverfault.com/questions/811912/can-nginx-location-blocks-match-a-url-query-string
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.
Trying to see if this is possible.
We have an app and a wordpress install.
Is it possible to use 2 locations for the same folder but under different circumstances. Example..
http://domain.com/subfolder/ - This shows the APP
http://domain.com/subfolder/anything - This shows WP permalink
Right now, we have it so
http://domain.com/subfolder (without the /) shows the app
http://domain.com/subfolder/ (witht the /) shows WP.
This does work, but would it be possible to have it so, it will only show WP IF the URL contains text after subfolder/*
Current Nginx conf:
location ^~ /knowledge {
root /opt/domain.com/public/;
try_files $uri #backend;
}
location /knowledge/ {
index index.php index.html index.htm;
root /opt;
include /etc/nginx/php-wpsc.conf;
try_files $uri $uri/ /knowledge/index.php?q=$uri&$args;
}
Obviously it makes sense to keep the location /knowledge/ block for WordPress, as that matches the majority of cases, with just one case that needs to be overridden.
A specific URI can be taken away from that location block by using a location block with a higher precedence. See this document for details.
One possibility would be an exact match location block:
location = /knowledge/ {
rewrite ^ /knowledge last;
}
Or possibly, change your existing location ^~ /knowledge block from a prefix location to a regular expression location, making the trailing / optional.
location ~ ^/knowledge/? { ... }
Note that this changes the order of evaluation of this location block, so there may be side-effects that need to be considered.