1

I've got a Jekyll generated site running on an Apache server and I'm having some trouble getting my .htaccess file set up correctly. Jekyll places index.html files into folders which represent each page so my URLs currently look like domain.com/foo/

I'd like to remove that trailing slash from the URL so that it exactly matches what I had set up previously (and also because I think it looks better).

Currently the section of my .htaccess file dealing with rewites looks like:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>

Options -Indexes

DirectoryIndex index.xml index.html

I have tried following the advice here but that puts me into a redirect loop.

Can anybody help me out? In brief, what I want is for a domain.com/foo URL to show the index.html file form the /foo directory and for domain.com/foo/ and domain.com/foo/index.html to redirect to domain.com/foo.

Community
  • 1
  • 1
mrappleton
  • 372
  • 3
  • 10

1 Answers1

6

You should be able to use this to turn off the addition of slashes.

DirectorySlash Off

Note that the trailing slash is added for a good reason. Having the trailing slash in the directory name will make relative URLs point at the same thing regardless of whether the URL ends with "foo/bar/index.html" or just "foo/bar/". Without the trailing slash, relative URLs would reference something up one level from what they normally point at. (eg: "baz.jpg" would give the user "/foo/baz.jpg" instead of "/foo/bar/baz.jpg", as the trailing "bar" will get removed if it isn't protected by a trailing slash.) So if you do this, you probably want to avoid relative URLs.

To then rewrite the directory name to return the index.html you could probably do something like this:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}/index.html -f
RewriteRule ^(.*)$ /$1/index.html [L]

This checks if REQUEST_URI/index.html exists, and if it does performs an internal redirect.

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
  • Thanks for the response, I think that might be half of the answer. That stops domain.com/foo from rewriting to domain.com/foo/ but it doesn't display the index.html file from within the /foo/ directory – mrappleton Jun 06 '12 at 20:17
  • @mrappleton You'll probably need to add a separate rewrite. I've added something that may work to the answer. – Laurence Gonsalves Jun 06 '12 at 20:28
  • That's pretty much got it. I just needed to add `RewriteRule ^(.+)/$ http://floatleft.com/$1 [R=301,L]` and I've got what I was after. Thanks so much for your help! – mrappleton Jun 06 '12 at 20:33
  • Oh one more thing this has thrown up. I have an index.xml in a /feed/ folder. Is there a way to point /feed at this too? – mrappleton Jun 06 '12 at 20:40
  • Oh not to worry - got it! Thanks again! – mrappleton Jun 06 '12 at 20:45