Bradley Oesch Bradley Oesch - 2 months ago 6
Apache Configuration Question

How to remove .html extension from url when .htaccess changes aren't working

I have a site I'm making with purely static html pages and I'd like to not have the .html extension on the webpages. I've read dozens of links with different ways to edit the .htaccess file to rewrite urls without the extension, but with no luck.

The strange thing is, I had the .htaccess file on my other site (bradleyoesch.com) and I can access pages without .html at the end (e.g. bradleyoesch.com/projects). However, I deleted the .htaccess file on that site and it still works (bradleyoesch.com currently has no .htaccess file), suggesting to me that I did something else two years ago to get it to work, or I need to restart something to pick up the changes (I've restarted apache on both servers with

sudo service apache2 restart
).

What am I missing or forgetting to do, or otherwise why would my .htaccess changes not work? My current .htaccess file contents are below.

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

Answer

Ok here's what finally worked. There were some edits to apache2 files I needed to do to get everything to work together. This assumes you're running apache2 version 2+ on your server with some form of linux.

Edit /etc/apache2/sites-available/000-default.conf to have the following lines, just append them to the end (I put them after this line: # vim: syntax=apache ts=4 sw=4 sts=4 sr noet).

<Directory /var/www>
    Options Indexes FollowSymLinks Multiviews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

Create or edit the .htaccess file in your root html directory (mine is /var/www/html and note the similar directory path above) with the following lines:

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

When you make any changes to an apache2 conf file, you have to restart apache2, and if you have not yet, you want to create some symlinks with a2enmod. Otherwise just restart apache2.

sudo a2enmod rewrite && sudo service apache2 restart

You do not need to restart anything when making changes to .htaccess. An easy way to check if it's loading .htaccess at all is to type gibberish in there to force a syntax error, then your pages will 500 error.