Skip to content

Instantly share code, notes, and snippets.

@mxhold
Last active January 21, 2019 00:10
Show Gist options
  • Save mxhold/00b076c996d6d74401c1 to your computer and use it in GitHub Desktop.
Save mxhold/00b076c996d6d74401c1 to your computer and use it in GitHub Desktop.
Prevent trailing slashes in URLs with Middleman and Nginx

So you want to have pretty URLs with no trailing slash or .html like:

http://mysite.com/blog/my-new-kitten

not like:

http://mysite.com/blog/my-new-kitten/

or

http://mysite.com/blog/my-new-kitten.html

In your Middleman config.rb file:

activate :directory_indexes
set :trailing_slash, false

In your Nginx config file:

server {
    ...

    location / {
        try_files $uri $uri/index.html =404;
        rewrite ^(/.+)/$ $1 permanent;
    }
    
    ...
}
@kstratis
Copy link

What about Apache?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment