Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the .html extension with Apache mod_rewrite

I have a small number of static sites where I simply want to hide the .html extension:

  • the url /foo fetches the static file /foo.html
  • the browser still displays the url /foo

The client can then send out bookmarks in the style mydomain.com/foo rather than mydomain.com/foo.html.

It sounds very simple, and I've used mod_rewrite happily before (say with WordPress or for redirects), but this is proving much harder to crack that I thought. Perhaps I'm missing something really obvious, but I can't find a solution anywhere and I've been at it all day!

We run our own server, so this can go wherever is the best place.

Addendum

The solution checked below worked fine. Then after running the site awhile I noticed two problems:

  1. all pages began to appear unstyled. I reloaded, cleared the cache, etc., but still no-style. I've had this trouble before, and can't locate the source.

  2. There's a directory AND an html file named 'gallery', so the /gallery link shows a directory listing instead of the html file. I should be able to sort that one, but further tips welcome :-)

like image 655
Dave Everitt Avatar asked Jan 02 '10 17:01

Dave Everitt


People also ask

How do I hide a HTML extension?

html extension can be easily removed by editing the . htaccess file.

What is mod rewrite in Apache?

mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. mod_rewrite is an Apache module that allows for server-side manipulation of requested URLs. Incoming URLs are checked against a series of rules. The rules contain a regular expression to detect a particular pattern.

What is Rewriteengine on Apache?

A rewrite engine is a component of web server software that allows you to rewrite or redirect uniform resource locators (URLs). The most popular rewrite engine is the Apache HTTP server's mod_rewrite. There are other web servers, such as nginx or lighttpd, that provide similar functions.


2 Answers

Try this rule:

RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L] 

This will rewrite all requests that can be mapped to an existing file when appending a .html.

like image 176
Gumbo Avatar answered Sep 21 '22 23:09

Gumbo


The previous answers don't check if the requested path is a directory.

Here is the full rewrite condition which doesn't rewrite, if requested path is a directory (as stated by the original question):

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d          # is not directory RewriteCond %{REQUEST_FILENAME}\.html -f     # is an existing html file RewriteRule ^(.*)$ $1.html                   # rewrite index to index.html 

To be SEO friendly and avoid double content, redirect the .html urls:

# Redirects domain.com/file.html to domain.com/file RewriteCond %{REQUEST_FILENAME} !-d          # is not directory RewriteCond %{REQUEST_FILENAME}\.html -f     # is an existing html file RewriteCond %{REQUEST_URI} ^(.+)\.html$      # request URI ends with .html RewriteRule (.*)\.html$ /$1 [R=301,L]        # redirect from index.html to index 

If you need the same for scripts take a look here: How can I use .htaccess to hide .php URL extensions?

like image 31
Stefan Profanter Avatar answered Sep 17 '22 23:09

Stefan Profanter