1

I seem to be stuck here trying to figure out how to redirect the following scenarios:

/foo/index.html    -> /foo (remove trailing slash, index.html as 301)
/foo/              -> /foo (remove trailing slash as 301)
/foo               -> No redirect, shows the file at /foo/index.html
/foo/notindex.html -> No redirect, shows the file at /foo/notindex.html

I believe the relevant lines in my .htaccess are

DirectorySlash Off
RewriteEngine On
RewriteBase /

# Redirect /foo/ to /foo
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L,E=LOOP:1]

# Redirect /foo to /foo/index.html
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^(.*)(\?.*)?$ $1/index.html$2 [L]

# Redirect /foo/index.html to /foo
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^(.*)\/index.html$ /$1 [R=301,L]

but of course, the contents of the entire .htaccess file could come in handy here too. What am I doing wrong?

UPDATE 11/13/14

I trashed all those rules above and am starting over:

# Remove trailing slash
DirectorySlash Off
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}/index.html -f
RewriteRule ^(.*)$ /$1/index.html [L]

This satisfies 2 of the 4 points. To be more specific:

1 https://server.com/foo            renders the /foo/index.html correctly without redirect
2 https://server.com/foo/           renders the /foo/index.html correctly but should redirect to /foo instead of rendering
3 https://server.com/foo/index.html renders the /foo/index.html correctly but should redirect to /foo instead of rendering
4 https://server.com/foo/foo.html   renders the /foo/foo.html correctly

So now that #1 and #4 are correct, how can I add 301 redirects for #2 and #3 without breaking it or looping?

1

It is not exactly the answer to your question but you should think twice about removing trailing slashes for directories.

The doc about DirectorySlash warns from potential problems (a mess with relative urls and others). And you can save some headaches by only redirecting with a 301 /dir/index.html to /dir/ (nearly your point 3), and using DirectoryIndex index.html directive.

  • This is the route I have chosen. Thanks for the input – coneybeare Nov 20 '14 at 0:47
1

Aren't your last two entries cancelling each other out? # Redirect /foo to /foo/index.html then the other way # Redirect /foo/index.html to /foo

If it just for those individual files? Then I'd try:

# 301 Redirect /foo/index.html to /foo
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^foo/index\.html$ /foo? [R=301,NE,NC,L]

# 301 Redirect /foo/ to /foo
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^foo/$ /foo? [R=301,NE,NC,L]

If your trying to do it for multiple files in one swoop, then try:

# 301 Redirect remove trailing index.html
RewriteCond %{THE_REQUEST} /index.html HTTP
RewriteRule (.*)index.html$ /$1 [R=301,L]

# 301 Redirect remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

You can also specify specific directories that you don't want too apply this rule to:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !I-want-a-trailing-slash-on-this-one/(.*)$
RewriteCond %{REQUEST_URI} !(.*)$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
  • Unfortunately, this does not work. It gives an Internal Service Error when rendering the page. Though I think you are right about the 2nd rewrite rule in my existing htaccess... – coneybeare Nov 13 '14 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.