Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I'm having some problems trying to

  1. Remove the .html extension from URLs
  2. Removing 'index.html' from an URL

1) To remove the extension I have tried using this in my htaccess file.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

However when I click links in my HTML such as <a href="abcde.html"></a> it doesn't remove the .html from the URL and I am left with www.website.com/abcde.html

2) I tried using this to remove the index.html

RewriteCond %{THE_REQUEST} \/index\.(php|html)\ HTTP [NC]
RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]

But when I load an index.html file on my server, my URL looks something like this

www.website.com/folder//

I am left with an extra / at the end.

Can anyone help me out?

share|improve this question
add comment

2 Answers

This is from stackoverflow

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
share|improve this answer
add comment

Building on the answer Anagio provided, let's cover all your requirements:

  • Transparently serve .html files on requests like /path/to/file if /path/to/file.html exists
  • Deny direct requests against /path/to/file.html
  • Serve /index.html on requests against /

Directory configuration:

Options +FollowSymLinks -MultiViews

DirectoryIndex index.html

RewriteEngine On
#
# Rewrite valid requests on .html files
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html?rw=1 [L,QSA]
#
# Return 404 on direct requests against .html files
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{QUERY_STRING} !rw=1 [NC]
RewriteRule ^ - [R=404]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.