How do I implement URL rewriting in my .htaccess file?

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

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

/site/.htaccess
/site/subfolder/.htaccess
/site/subfolder/cats/.htaccess

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!


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.