rewrite all .html extension and remove index in Nginx
- by Pardoner
How would I remove all .html extensions as well as any occurrences of index.html from a url string in Nginx
http://www.mysite/index.html to http://www.mysite
http://www.mysite/articles/index.html to http://www.mysite/articles
http://www.mysite/contact.html to http://www.mysite/contact
http://www.mysite/foo/bar/index.html to http://www.mysite/foo/bar
EDIT: Here is my conf file:
server {
listen 80;
server_name staging.mysite.com;
root /var/www/staging.mysite.com;
index index.html index.htm;
access_log /var/log/nginx/staging.mysite.com.log spiegle;
#error_page 404 /404.html;
#error_page 500 503 /500.html;
rewrite ^(.*/)index\.html$ $1;
rewrite ^(/.+)\.html$ $1;
rewrite ^(.*/)index\.html$ $scheme://$host$1 permanent;
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
location / {
rewrite ^/about-us /about permanent
rewrite ^/contact-us /contact permanent;
try_files $uri.html $uri/ /index.html;
}
}