Apache Removing .html

Introduction

I have been going through a major revamp of my website lately, modernizing URLs, moving folders, etc. One thing I wanted to do was remove the .html from the URL whenever you go to a new website. I was looking up ways to do it, and it all led back to Apache's mod_rewrite plugin.

To clarify, removing the .html extension means you see cindybui.me/pages/blogs/apache_mod_rewrite in your address bar right now, not cindybui.me/pages/blogs/apache_mod_rewrite.html. And you can go to cindybui.me/pages/projects/cs as opposed to cindybui.me/pages/projects/cs.html. The file is an html file, but we can hide the extension for aesthetic purposes to make it look cleaner.

I looked at this stack overflow post talking about mod_rewrite, this stack overflow post talking about mod_rewrite and .htaccess, this digital ocean blog, and of course the apache documentation on mod_rewrite, htaccess, and the rewrite introduction webpages.

A lot of sites say to use .htaccess. However, apache says not to use it unless you don't have access to the apache configuration files. If you followed my web hosting guide, then you have direct access to the apache configuration files.

Solution 1 - Root Access

This is the solution I ended up going for. Run

sudo vim /etc/apache2/apache2.config
and modify your config file to have this:
<Directory /home/cindy/www>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+)$ $1.html [L,QSA]
</Directory>
Replacing the username with your username. Once those settings are done, restart apache with this line:
sudo systemctl restart apache2

Done!

Solution 2 - .htaccess

Do not follow this solution if solution 1 worked for you. htaccess files, as specified by apache, are only supposed to be used if you don't have access to the config files. This will also take a performance hit since apache has to parse the config file first then each .htaccess file. However, just for completeness, I'll show it here. It's essentially dividing the commands into two separate files.

Make sure your config file has AllowOverride All, which you'll have to ask your administrator for if it's not set. They will run

sudo vim /etc/apache2/apache2.config
and then put this into the Directory tag:
<Directory /home/cindy/www>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
Your server administrator will have to run
sudo systemctl restart apache2
for the changes to take effect. The important part here is AllowOverride All. This allows the .htaccess file to override the settings. Then, go into your directory with your website. Mine is at ~/www/html/cindybui.me. Make a new file called .htaccess and put this text into it:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

binaryfunt's answer on StackOverflow has an amazing description of what exactly these regex commands mean if you're interested.