1

How do I get rid of the trailing slash on my site?

For example, I have an index.html at

 http://example.com/examplepage/index.html 

Instead of

 http://example.com/examplepage/ 

I would like the URL to be

 http://example.com/examplepage 

Could I do this with .htaccess?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
Zooza S
  • 307
  • 1
  • 4
  • 17

1 Answers1

1

The trailing slash is really important for apache, without it, even if you have an index.html sitting in the examplepage folder, people will be able to see the contents of your folders. Apache deals with this by having a module loaded by default that redirects the browser to include the trailing slash everytime a directory/folder is accessed. You can turn that off but it's noted in the documentation that there's a major security concern when you do that; mainly, the contents of your folders can be viewed regardless of having an index file or not.

So you can turn this off, but you probably want to still have the trailing slash at least internally. You can do that with mod_rewrite:

# turn off the mechanism to always redirect to the trailing slash
DirectorySlash Off

# Internally add the trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond $1 .*[^/]$
RewriteRule ^(.*)$ /$1/ [L]

That should allow you to access http://example.com/examplepage without getting redirected to http://example.com/examplepage/.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220