Force Lowercase URLs in Apache

- 2 minutes read

Forcing your URLs to lowercase (or another unified format) is essential to preventing duplicate links on your site.

And for most sites - especially content sites, this is a critical component of SEO and great user experience.

First, you will need to enable Apache’s “rewrite” library with the following command:

sudo a2enmod rewrite

But if it was not previous enabled, you’ll then need to restart Apache, if you are certain it is safe to restart at that time:

sudo service apache2 restart
sudo systemctl restart apache2

Once you’ve done that, there’s two common ways to force your URLs to lowercase.

Link to this section Addding a rule to your .htaccess file

One option is to add these rules to your .htaccess file:

RewriteEngine On
RewriteMap lowercase int:tolower

With this approach, you won’t need to restart Apache. Your URLs will be automatically rewritten to lowercase right away!

Link to this section Modifying your Apache configuration directly

Alternatively, there’s a similar approach you can take, if you have access to your server’s Apache configuration file.

Simply insert the same two lines just before the end of your primary <VirtualHost> tag:

<VirtualHost *:80>
    ...
    RewriteEngine On
    Rewrite Map lowercase int:tolower
</VirtualHost>

If it’s safe to restart your web server at that time, you’ll then need to restart Apache once more.

Link to this section Conclusion

That’s all! I always keep this snippet on hand for when I’m spinning up a new Apache server.

Thank you for reading, and I hope this article helped!