0

There are so many pages on stackoverflow and elsewhere about Redirect. However, I couldn't find a solution to a simple problem: I want every request that ends with a slash to have a permanent 301-redirect to */index.

Examples:

http://example.com/ - should redirect to http://example.com/index

http://example.com/foo/ - should redirect to http://example.com/foo/index

http://example.com/foo/bar/ - should redirect to http://example.com/foo/bar/index

etc.

However, requests not ending with slash should not be redirected:

http://example.com/foo - no redirect.

http://example.com/foo/bar - no redirect.

Community
  • 1
  • 1
mdthh
  • 1,248
  • 1
  • 13
  • 22

1 Answers1

2

Give this a try and see how it works for you.

DirectorySlash Off

RewriteEngine On 
RewriteRule ^(.*?)/$ http://example.com/$1/index [R=302,L]

Change [R=302,L] to [R=301,L] when you're sure it works for you.

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • Thanks, this works _almost_. It redirects http://example.com/foo/ to http://example.com/var/www/foo/index ... – mdthh Mar 02 '14 at 12:03
  • O.K., this works for me now: `RewriteRule ^(.*?)/$ http://example.com/$1/index [R=302,L]` Do you wish to edit your answer so I can mark it as accepted answer? – mdthh Mar 02 '14 at 12:14
  • 1
    Yes you could also use / in front of $1 to fix that also. Edited. – Panama Jack Mar 02 '14 at 15:58