Jump to main navigation


Removing index.html with mod_rewrite in .htaccess

121029

It's quite easy to remove index.html from a URL with mod_rewrite. Let's say we want to redirect www.dense13.com/index.html to www.dense13.com:

RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]

Here's a brief explanation, if you're not familiar with .htaccess syntax:

  • The RewriteRule directive has three parts, a pattern (^index\.html$), a substitution (/) and optionally some modifiers ([R=301,L]).
  • In the pattern: the symbol ^ means "start with", and the symbol $ means "ends with". Also, the backslash is the escape character, and we need to put it in front of the dot, because the dot normally has a special meaning, and we don't want that here. So in this case the pattern will only match the string "index.html".
  • If the pattern is found (that is, if the request is to index.html), it will be redirected to "/", which is the root of your website.

Always remove index.html

What if you want to always remove index.html? For example, www.dense13.com/music/index.html -> www.dense13.com/music/ . Easy!

RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

The second rewrite rule checks for any request that ends with /index.html, and removes the index.html bit. Again, quick explanation of the second rewrite rule:

  • (.*) - This means 'anything'. The dot means 1 character, and the asterisk means "zero or more". So, "zero or more characters". We put this between parentheses because we'll need this in the substitution (keep reading).
  • In the substitution, $1 means "whatever was between parenthesis in the pattern", which is anything before the /index.html .

Sorted! You might need little adjustments if you have more specific needs, but hopefully this gives you a good starting point.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 comments to “Removing index.html with mod_rewrite in .htaccess”

  1. #01 By ajith, 140320 at 22:34

    RewriteEngine On RewriteRule ^index\.html$ / [R=301,L] RewriteRule
    ^(.*)/index\.html$ /$1/ [R=301,L] i rewrite th htaccess with this
    ....but i can't remove .html extension

  2. #02 By Simon, 150212 at 19:07

    Can I remote the html after a forward? RewriteRule ^services/?$
    "http://www.domain.com/services.html" [R=301]

  3. #03 By maki, 180205 at 17:49

    thank you so much for this information, now i understand a little
    about htacess. THANK YOU SO MUCH!!!

Add a comment


Allowed XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <p> <strike> <strong>


To prove you're a person (not a spam script), type the answer to the math equation shown in the picture. Click on the picture to hear an audio file of the equation.
Click to hear an audio file of the anti-spam equation =

Additional content and navigation

Categories

Main navigation menu