Mobile app version of vmapp.org
Login or Join
Samaraweera270

: Remove trailing slash and index.html in .htaccess I seem to be stuck here trying to figure out how to redirect the following scenarios: /foo/index.html -> /foo (remove trailing slash,

@Samaraweera270

Posted in: #Apache #Htaccess #ModRewrite #Redirects

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 ^(.*)/(?.*)?$ [R=301,L,E=LOOP:1]

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

# Redirect /foo/index.html to /foo
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^(.*)/index.html$ / [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 ^(.*)$ //index.html [L]


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

1 server.com/foo renders the /foo/index.html correctly without redirect
2 server.com/foo/ renders the /foo/index.html correctly but should redirect to /foo instead of rendering
3 server.com/foo/index.html renders the /foo/index.html correctly but should redirect to /foo instead of rendering
4 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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Samaraweera270

2 Comments

Sorted by latest first Latest Oldest Best

 

@Gonzalez347

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.

10% popularity Vote Up Vote Down


 

@Megan663

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$ / [R=301,L]

# 301 Redirect remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)$
RewriteRule ^(.*)$ www.example.com/ [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 ^(.*)$ www.example.com/ [R=301,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme