Wordpress htaccess setup

What is htaccess

Htaccess is a file that config apache server. We can make our website more secure and faster. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.

You can google htaccess configuration online and you will find plenty of them. Below I’ll show my configutation.

Find your htaccess

In your cpanel dashboard, go to file directory. However, hidden files will not be displayed. In order to do that, we go to our URL and add showhidden=1. This configuration will display our htaccess.

Setup code snippets

Here are some code snippets that I use for my wordpress site.

  1. This prevents anyone to visit wp-config

    1
    2
    3
    4
    <files wp-config.php>
    order allow,deny
    deny from all
    </files>
  2. Prevent Directory Browsing

    1
    2
    # disable directory browsing
    Options All -Indexes
  3. Enable Browser Cache. Basically if the image remains unchanged, then it will be stored on client’s browser and will not need to reload. This will improve the user experience.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    ## EXPIRES CACHING ##
    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 2 days"
    </IfModule>
    ## EXPIRES CACHING ##
  4. No Hot linking to your images. Outside can not link to your images. This will reduce exccess traffic.

    1
    2
    3
    4
    5
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourwebsite.com [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourotherwebsite.com [NC]
    RewriteRule \.(jpg|jpeg|png|gif)$ http://i.imgur.com/g7ptdBB.png [NC,R,L]

References