Useful snippets for your .htaccess file

I am creating and editing .htaccess files quite often, at least when I start a new WordPress project. It is very useful to manage, set up and/or protect your web site with a few code lines.

In this article you will find the most useful code snippets often used in a .htaccess file.

If something goes wrong, no problem, instead of your website you will see an ERROR500 message. No problem, just change back to the previous working .htaccess file – I guess you always do a backup, right?

About .htaccess

.htaccess is the default file name of a special configuration file. It provides commands for controlling and configuring the Apache Web Server. It controls and configures modules that can be built into the Apache installation, or included at run-time like mod_rewrite, mod_alias and mod_ssl. Well, I kept the story very short.

Redirecting for SSL certificate

When you will install a SSL certificate, you want all traffic over HTTPS. Sometimes it just works, sometimes you have to force it over a permanent redirect (301) like this:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]

Redirecting, temporary and permanent

This is quite important for SEO and to let you know the search engines what have been redirected and what is the actual link. Permanent redirect is 301, temporary is 302.

So, this allows you to permanently (301) redirect your entire website to any other domain

Redirect 301 / http://yourdomain.com/

Redirecting temporarily (302) is good for SEO purposes when you plan to switch the website back

Redirect 302 / http://yourdomain.com/

Redirecting original links if they are not existing anymore, you decided to provide new or different content with different link (URL address)

Redirect 301 /olddirectory/oldfile.xyz http://yourdomain.com/newdirectory/newfile.xyz

Allow Origin

Sometimes when you are using CDN, or loading custom fonts (or icons), you can see weird characters. To fix it, use

<FilesMatch “\.(ttf|otf|woff)$”>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin “*”
</IfModule>
</FilesMatch>

Custom error page 404 Not found

If you created a page with link “not found” for pages which are not existing (non-valid links) you can do redirect – set up custom page. Well, nowadays most of WordPress themes has custom error page built-in.

ErrorDocument 404 errors/yournotfound/

Add Expires headers

It is very useful to set up an expiry time for files (images, attachments, scripts, etc) from the point of SEO. The code with seconds for it is

<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault A432000
ExpiresByType text/css A777600
ExpiresByType image/gif A777600
ExpiresByType image/png A777600
ExpiresByType image/jpg A777600
ExpiresByType image/x-icon A777600
ExpiresByType application/x-javascript A777600
ExpiresByType text/plain A777600
</ifModule>

If you are having frequently updated content, set it on 7 days or less, for news can be set 0. I am using for my needs 9 days, it is exactly 777600 seconds.

You can use also this for everything with one time

<ifModule mod_headers.c>
ExpiresActive On
<filesMatch “.(htm|html|css|txt|ico|gif|jpg|jpeg|png|mp4|webm|ogv|woff|eot|svg|ttf|js)$”>
Header set Cache-Control “max-age=777600, public, must-revalidate”
</filesMatch>
</ifModule>

Protecting wpconfig.php file

It is good to protect this very important configuration file by restricting access to it.

<files wp-config.php>
order allow,deny
deny from all
</files>

Extra protection against SQL injection

This should be covered by existing WordPress version, but in case you are using some extra stuff on your website, it is not a bad idea also to drop it into a .htaccess file

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]

Restrict directory listing

It is nobody’s business what is on your server. Some files can be just temporary for testing etc, so it is good to prevent listing files with

Options -Indexes

Time zone

For some purposes you want simulate different time zone you can do it with snippet below. Full list of supported time zones is here.

SetEnv TZ Australia/Sydney

Gzip compression

Gzip is normaly enabled already. But if you have web hosting with a company which doesn’t care about services, you may find no gzip compression. If it is supported, you can enable it.

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/html
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

And now the best tip ever!!!

Actually it is simple: do backup, backup and backup. When something goes wrong, you can always roll back without having nervous breakdown.