3

I would like to get rid of all the file extensions on my site. Except when they are on the index i would like it to say nothing...

change this foo.com/index.html
to this foo.com/

and when the user goes to another page like foo.com/contact-us.html
it will be foo.com/contact-us

RewriteEngine On
RewriteRule ^ this is where i get confused :(

Thanks in advance!

PHPNooblet
  • 687
  • 1
  • 7
  • 15

3 Answers3

3

Try these rules:

RewriteRule ^index\.html$ / [L,R=301]
RewriteRule (.+)\.html$ /$1 [L,R=301]
Gumbo
  • 643,351
  • 109
  • 780
  • 844
2

I'd use a slightly different method:

RewriteCond %{REQUEST_FILENAME}\.html -f # if request.html exists and is a file
RewriteRule ^(.+)$ $1\.html [QSA] # then rewrite to that file.
TRiG
  • 10,148
  • 7
  • 57
  • 107
  • @TRiG: +1 very interesting. When browser asks for `http://domain.com/test` the server serves out `http://domain.com/test.html` without the url in browser address being changed because we are not redirecting. Did I understood properly? – Marco Demaio May 19 '11 at 13:37
  • 1
    @Marco, Yes, that's it. When the browser asks for `/test` the server looks for a file called `test.html` and serves that. If the `test.html` file doesn't exist, the server looks for a file or directory called `test` and serves that. Failing that it'll give the default 404 page. – TRiG May 19 '11 at 15:03
  • @TRiG: thanks for the explanation. Last question, when user asks for `foo.com/index.html` would the server simply serves out `foo.com/index.html` with no rewrites cause the `RewriteCond %{REQUEST_FILENAME}\.html -f` is NOT met?! – Marco Demaio May 19 '11 at 15:30
  • @Marco, A request for `/index.html` should cause Apache to look for the file `index.html.html`. Assuming it doesn't find it, it'll go on to serve `index.html` as normal. So unless you have any `*.html.html` files knocking around, a request for `/example` and a request for `/example.html` will be equivalent – TRiG May 19 '11 at 15:57
  • @TRiG: understood thanks. You seem to be a mod_rewrite expert, if one day you have time could you plz help me with this other question http://stackoverflow.com/questions/6059920/removing-index-html-from-url-and-adding-www-with-one-single-301-redirect – Marco Demaio May 19 '11 at 15:59
  • @Marco, Far from it. You need someone who's better at regex than I am to be a `mod_rewrite` expert. – TRiG May 20 '11 at 10:19
1

It doesn't have to be so complicated, apache has an option to accomplish what you want:

Just add this rule to your .htaccess and /contact-us/ will automatically be rewritten to contact-us.html:

Options MultiViews

That's it!

  • van del Bogend: I heard it does not work when browser that request pages to server is IE. – Marco Demaio May 19 '11 at 16:00
  • That's not true, it's a server side thingy, so it has nothing to do with the browser. As long as the headers are correct, no browser will complain. I'm using it on large scale and have not had any complaints about it. – Robbert van den Bogerd May 24 '11 at 10:23