rewrite all .html extension and remove index in Nginx

Posted by Pardoner on Server Fault See other posts from Server Fault or by Pardoner
Published on 2012-10-16T20:21:40Z Indexed on 2012/10/16 23:03 UTC
Read the original article Hit count: 351

Filed under:
|

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;
    }

}

© Server Fault or respective owner

Related posts about nginx

Related posts about rewrite