Use .Htaccess To Optimize Your WordPress Website


The .htaccess is often used to add, modify and override server level configurations and website’s security and performance. In many cases, many people are not aware of the full potential of this file. In this article, I will highlight several interesting things you could accomplish using the .htaccess file.

The following is the default code in a typical single-site WordPress .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L
# END WordPress

The following is the default code in a typical WordPress multi-site .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

Protect WordPress Admin Area

One of the great ways of protecting the admin area of a WordPress website is to limit access only to a selected pool of IP.

AuthUserFile /Custom Folder/null
AuthGroupFile /Custom Folder/null
AuthType Basic
order deny,allow
deny from all
# whitelist Saud's IP address
allow from
# whitelist Mustaasam's IP address
allow from

Remember to replace xx values with your own IP address.

WWW Redirection

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L

Protect Important Files

You could use .htaccess to protect important files including error logs, wp-config.php and php.ini:

<FilesMatch "^.*(error_log|wp-config\.php|php.ini|\.[hH][tT][aApP].*)$">
Order deny,allow
Deny from all

Related Post

Latest Post

Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.