I'm totally baffled with this one. Its probably something silly, and I'm missing it after along day! Anyway, I have this rewrite rule setup in my nginx config for the site:
location / {
root /srv/www/site.co.uk/www;
index index.html index.htm index.php;
rewrite ^/(.*)/index.html$ http://site.co.uk/$1/ permanent;
rewrite ^/index.html$ http://site.co.uk/ permanent;
}
When I go to:
http://www.example.com/index.html
http://www.example.com/foo/index.html
..then it correctly sends to:
http://www.example.com/
http://www.example.com/foo/
If I comment those 2 rewrite rules out, restart nginx, then retry... the page loads fine!
Can anyone see where I'm going wrong? Maybe I'm just being blind!
You have constructed a rewrite loop.
The index directive effectively generates an internal rewrite to /index.html whenever a URL with a trailing / is presented.
One way to break the loop is to only apply your rewrite rules when the external URL contains index.html. The variable $request_uri contains the external URL and can be tested using an if directive. See this caution regarding if.
if ($request_uri ~* "/index\.html(?|$)") {
rewrite ^(.*/)index\.html$ $scheme://$server_name$1 permanent;
}
location / {
root /srv/www/site.co.uk/www;
index index.html index.htm index.php;
}
Related
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.
I want to write all the redirections of my website pages in one location block
This is what I have done so far
location /mywebsite {
rewrite ^/mywebsite$ /index.php;
rewrite ^/mywebsite/about$ /about.php;
rewrite ^/mywebsite/services$ /services.php;
rewrite ^/mywebsite/events$ /events.php;
rewrite ^/mywebsite/events/(.*)$ /events.php?id=1 last;
}
but this does not work as intended.
I am trying to set the rewrite rules in NGINX and I currently have some set for
location / {
already for my main website on that domain.
I would like to insall wordpress in a different folder so my location / {
needs to be location /page {
I can't seem to set the rewrite rules up for this?
Any help would be great I have tried.
Rewrite rules for wordpress 3.0 (multi-site) for nginx?
Doesn't seem to work.
Using this < I can get the page to show up for each user but with no images, css all the other files needed. Its only in plain text.
location /page {
root /home/domain/public_html;
index index.html index.htm index.php;
if (!-e $request_filename ) {
rewrite ^/page/(.*)$ /page/1$ last;
}
}
I need some rules that can be used for the /page location.
You have a small typo:
Rather than using:
rewrite ^/page/(.*)$ /page/1$ last;
Please try:
rewrite ^/page/(.*)$ /page/$1 last;
Regex captures value is $1, not in 1$ ;-)
You will need to change few more things. Here is a complete config - http://rtcamp.com/tutorials/wordpress-nginx-multisite-subdirectories-in-subdirectory-itself/
For my subdomain I wanted to point to a different robots.txt file. I had hoped the following code would work:
if ($host ~ subdomain) {
rewrite ^/favicon.ico$ /favicon.ico break;
rewrite ^/robots.txt$ /robots.subdomain.txt break;
rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2&$query_string last;
}
favicon.ico works fine, all other extensions are rewritten to index.php just fine, but so is robots.txt.
I spent [wasted] a lot of time trying to solve it, which I did by adding the following line after the robots.txt rewrite.
rewrite ^/robots.subdomain.txt$ /robots.subdomain.txt break;
Can someone please help me why it only works when I add this line, also any improvements to my config would be welcomed if you see any obvious inefficiencies! Thank you.
This should be what you're looking for:
location / {
rewrite ^/robots.txt$ /robots.$host.txt; # rewrites robots.txt
try_files $uri $uri/ #php; # try_files serves statics and sends everything else
# to your front controller (index.php)
# files such as favicon.ico get served normally
}
location #php {
rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2 last;
}
The only caveat is that your robots.txt needs to be named after the full host, so in the example above your www.domain.com needs to have a robots.www.domain.com.txt file in the document root. This seems a bit ugly, so I'd do it this way instead:
rewrite ^/robots.txt$ /$host.robots.txt;
and then you name your file www.example.com.robots.txt
Can someone help me for my nginx rewrite rule. I have the problem like this
if file not found in www.abc.com/name_dir/* it will redirect to www.abc.com/name_dir/index.php .
for example :
not found in www.abc.com/xxx/* redirect to www.abc.com/xxx/index.php
not found in www.abc.com/yyy/* redirect to www.abc.com/yyy/index.php
not found in www.abc.com/zzz/* redirect to www.abc.com/zzz/index.php
not found in www.abc.com/kk/* redirect to www.abc.com/kkk/index.php
...
the problem i have thousand of name_dir. I have nginx.conf like this
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
rewrite (^.+$) $1/
break;
}
if (!-e $request_filename) {
rewrite ^/xxx/(.*)$ /xxx/index.php?$1 last;
rewrite ^.+?(/.*\.php)$ $1 last;
}
In configuration above only redirect name_dir xxx. How rewrite rule to redirect all directory ?
Thank for your help
You want to use try_files to check for the existence of files instead of if statements here (because If's are Evil in Nginx).
To to a single directory, it would be like:
location /xxx/{
try_files $uri $uri/ /xxx/index.php;
index index.php
}
What this does is try the uri as a file first. If that doesn't work, it'll try as a directory. If neither work, it'll default to index.php of /xxx/. The extra index line is to keep it from showing a blank page if you go directly to whatever.com/xxx
Using regex, we can expand this rule to work with more than one directory:
location ~* ^(/.*)/{
try_files $uri $uri/ $1/index.php?$uri&$args;
index index.php
}
This should grab the full directory structure and rout it to the appropriate index.
abc.com/yyy/nonexistant.php ==> abc.com/yyy/index.php
abc.com/yyy/zzz/nonexistant.php ==> abc.com/yyy/zzz/index.php
If you only wanted the second example to go to yyy/index.php, use this regex in the location instead:
^(/.*?)/