I'd like to do some URL rewriting (Why? See this question.) so that instead of users seeing addresses like


they can instead see and use simply


(Even better, maybe I should restructure that so that it's courses/compilers.)

I'm using a Linux-based shared hosting service for my website, so I do not have administrative control of the server, but I do have control over .htaccess. The references I've read online seemed less than clear to me, so I'm looking for a little clarity and advice here.

  • Note that this is not something you only do in the server config (.htaccess). The important first step is to actually change the URLs in your application, otherwise you will end up with a lot of unnecessary redirects (or duplicate content).
    – MrWhite
    Commented Dec 16, 2015 at 1:21
  • See this : Apache htaccess tutorial for beginners helponnet.com/2021/04/15/htaccess-tutorial-for-beginers
    – starkeen
    Commented Jul 17, 2021 at 10:11

4 Answers 4


You could actually remove .html from the files on your server and set settings in htaccess so that they get served up as html files, but that's probably not what you're looking for.

Do this :

RewriteEngine on
# Check this condition to ensure that it's not a directory.
RewriteCond %{REQUEST_FILENAME} !-d
# Check this condition to ensure that there's a file there
RewriteCond %{REQUEST_FILENAME}\.html -f
# Replace html with your file extension, eg: php, htm, asp
RewriteRule ^(.*)$ $1.html
  • There is more complexity here, especially if you are worried about duplicate content problems. With the rule above, your pages will still be accessible from the old URLs.
    – JasonBirch
    Commented Jul 9, 2010 at 22:12

First, make sure apache has the module and that it is allowed in .htacces files, then add:

RewriteEngine on

# Rewrite rules
RewriteRule ^/myoldsubfolder/(.*) /newfolder/$1 

Your example would require

RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI}\.html -f
RewriteRule ^/(([a-zA-Z0-9_]+)/?)$ /$1.html

For a single-level directory structure and

RewriteRule ^/(([a-zA-Z0-9_]+)/?)+$ /$1.html

For a multi-level structure.

But be careful

If you have the structure


The rewrite rules in the child .htaccess files will be relative to their parent folder and you can get into some messy rewrite loop situations. Make sure you use RewriteCond to stop them happening if you can!

  • Do you mind going into more detail on that last point (using RewriteCond to stop the loops)? Commented Jul 9, 2010 at 13:30

As addition to above answers one can use

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([\w-]+)$ $1.html [L]

([\w-]+) Allows letters, digits with a hyphen (-).

(.*) will rewrite every request regardless, which may impotent other following rules causing internal server error on accessing.

  • 1
    Your RewriteRule pattern is an improvement over the accepted answer for the specific example given in the question, however, it will fail to match the more general case when there is more than one path segment, eg. /path/to/course-compilers. Include the L flag to lessen the impact on the following rules. Whilst the (.*) pattern is unnecessarily general, it is unlikely to cause serious problems, because of the condition that checks the file exists.
    – MrWhite
    Commented Feb 8, 2017 at 11:22

You don't have to use an .htaccess file to accomplish this. Simply structure your files into directories, and instead of using "labouseur.com/course-compilers.html", just name the file index.html and put it into a directory called "course-compilers". Then when you point to that directory, the index is served up by default but will not be displayed in the URL. So you could just write your links as "labourseur.com/course-compilers" and all will be well.

No .htaccess required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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