4

For aesthetic reasons (I prefer when there is a strict one-to-one mapping between URL and pages), I do not like the fact that http://www.example.com/index.html and http://www.example.com/ yield the same content with two different URLs. I would like to have http://www.example.com/ as the canonical one.

The obvious solution:

Redirect permanent /index.html /

is wrong (endless loop).

A better solution? It seems surprisingly difficult.

bortzmeyer
  • 3,941
  • 1
  • 21
  • 24

6 Answers6

2

Try a RewriteCond from mod_rewrite:

RewriteEngine On
RewriteCond %{QUERY_STRING} !^/$
RewriteRule /index.html / [R]

This says if the query string is not / then rewrite /index.html as / (should not loop).

PP.
  • 3,316
  • 6
  • 27
  • 31
  • Thanks, clearly the best solution (I adapted it a bit and, since I do not have enough points to edit your answer, I created mine). – bortzmeyer Aug 29 '10 at 21:15
2

My solution (which seems to work so I accepted it), inspired from PP's response, is:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/index\.html$
RewriteRule .* http://www.example.com/ [R=302,L]

Any non-modrewrite solution? I had to activate a new Apache module, which I try to avoid.

bortzmeyer
  • 3,941
  • 1
  • 21
  • 24
1

RedirectMatch ^/index.html$ http://www.example.com/

joschi
  • 21,387
  • 3
  • 47
  • 50
1

Why not set DirectoryIndex to something else - e.g., unpredictable.html, and name your index file similarly?

DirectoryIndex unpredictable.html
Redirect permanent /index.html http://yoursite/

You need never expose the chosen DirectoryIndex value.

Note that the last argument to Redirect needs to be a full URL, not just a path fragment.

jmtd
  • 575
  • 2
  • 6
  • Nice and clever idea but, still, if someone uses the /unpredictable.html URL, it will get to the content of the index page. – bortzmeyer Aug 25 '10 at 09:43
1

A non mod_rewrite solution… Actually joschi nearly had it but for some (unclear to me) reason you have to kludge around the endless loop:

RedirectMatch permanent ^/index\.html$ http://www.example.com/
AliasMatch ^/$ /var/www/index.html
kmkaplan
  • 111
  • 1
  • 2
0
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/index\.html$
RewriteRule .* http://www.example.com/ [R=302,L]

perfect solution

Jenny D
  • 27,780
  • 21
  • 75
  • 114