NGINX rewrite/location regex redirect /index.html$ -> / -
i want maintain 1 single url pages , i'm using index index.html directive have page @ /writing/index.html displayed when visits /writing/. however, index directive /writing/index.html still valid url nginx serves page at.
i want /writing/index.html 301 redirect /writing/, , forth root path (/index.html -> /) , other urls (/foo/bar/index.html -> /foo/bar/).
i want use regular expression matches /index.html ending such as: ^(.*/)index\.html$
but if if add
rewrite "^(.*)/index\.html$" $1 last; to nginx conf i'm seeing /writing/index.html 301 redirect /writing/ see /writing/ 301 redirect /writing/ in infinite loop.
so question why above rewrite regex match /writing/ when not end in index.html? because of internal index directive in nginx conf?
i've seen other 1 off solutions on stackoverflow redirecting single path, not solution in clean/generic way this.
below 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/; }
so solution problem have rewrite inside location checks against actual request's $request_uri avoids internal re-routing index directive.
pretty use instead:
if ($request_uri ~ "^(.*/)index\.html$") { rewrite "^(.*/)index\.html$" $1 permanent; }
Comments
Post a Comment