I want to maintain one single URL for all pages and I'm using the index index.html directive to have a page at /writing/index.html be displayed when someone visits /writing/. However, with this index directive /writing/index.html is still a valid URL that nginx serves a page at.
I want /writing/index.html to 301 redirect to /writing/, and so forth for the root path (/index.html -> /) and all other URLS too (/foo/bar/index.html -> /foo/bar/).
I want to use a regular expression that only matches the /index.html ending such as: ^(.*/)index\.html$
But if if I add
rewrite "^(.*)/index\.html$" $1 last;
to my nginx conf I'm seeing /writing/index.html 301 redirect to /writing/ which is good but I also see /writing/ 301 redirect to /writing/ in an infinite loop.
So my question is why does that above rewrite regex match /writing/ when it does not end in index.html? Is it because of the internal index directive in the nginx conf?
I've seen other one off solutions on StackOverflow for redirecting a single path, but not a solution that does it in a clean/generic way like this.
Below is my current nginx.conf
server {
listen 80;
server_name example.com *.example.com;
charset utf-8;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
rewrite "^(.*/)index\.html$" $1 permanent;
location / {
root /srv/www/example.com/;
index index.html;
}
error_page 404 /404/;
}
indexdirective. Any idea on how to prevent that from happening though?