What should you have in your .htaccess?
Whilst many people say that .htaccess can slow down your website (here is another interesting article on it), which is true, chances are that if you run a website on an Apache server you’ll have one, and you’ll use it, and frankly, the slow down is only a few milliseconds even for a huge .htaccess file.
So, what do you include in it?
HTTPS and www. sub domains
Firstly, if your site uses HTTPS (which it should to rank higher in SEO rankings), you’ll want to force HTTPS across all pages.
This snippet will force HTTPS on all pages of your site.
But what about if you want the www. sub domain too? I use this as it means that cookies aren’t carried across to other sub domains. For this, you’ll want to use the following snippet (that enforces https too):
Pretty links
I also like to remove the .html
line endings on my sites I build, and you can do this by enabling multiviews like so:
This makes links a lot cleaner - simply refer to links as <a href="page">
instead of <a href="page.html">
.
Site security
Securing your site is also very important. Whilst most hosts disable access to directory listing and .htaccess, I always like to make sure that everything is prevented from being viewed by the public. Therefore:
You can also block access to files such as .env
by using this:
Caching and site speed
Caching is also important for websites. I like to enable gzip on most of my sites, as well as set expiry headers for images and other website objects that are unlikely to change.
You can enable gzip like so. Be sure to include or exclude files you want to compress.
You can set caching like so:
Whilst the above way is longer, and you could cut down on lines by using mod_headers
, I like to do it the above way as it makes it easier to change caching lengths are more readable.
Custom error pages
Custom error pages are incredibly important - they make sites look more professional, and increase user retention. I usually make a few custom error pages for things such as PHP errors, 404s, 403s, and a few more. Here is an example of how you can refer to them in case a user encounters an error:
The above links to error pages under the errordocs
folder. I like to separate error pages from the root directory.
Site under maintenance
If you ever want to make a large change to your site, you can redirect users to a certain page, or even a different website entirely when the main one is under maintenance. This is also great if you are having issues on a new version of a Wordpress template, or are updating your online store.
This will redirect everyone but your IP and 127.0.0.1
to that page.
301 redirection
If you move domain, you’ll want to redirect users to your new domain. You can do this with .htaccess:
.htaccess is a great tool for those using Apache servers. It allows you to have so many options on configuring your server, and unless you are running a very basic site, is a must have. Hopefully these snippets will come in handy for your site!