Understanding and Customizing htaccess File for a WordPress blog

We are influencers and brand affiliates.  This post contains affiliate links, most which go to Amazon and are Geo-Affiliate links to nearest Amazon store.

The .htaccess file is the standardized Apache specific configuration file and is leveraged by most hosting providers that support WordPress blogs.  Unless you are running on a Windows hosted environment with IIS servers and therefore you wouldn’t have a .htaccess file chances are your WordPress blog is using a .htaccess file to handle everything from how the site content is structured, URL’s are rewritten to any custom redirects you may have put in or leveraged plugins to handle.


Many WordPress plugins will write their own entries to the .htaccess file as well, some are more graceful and use comment tags to indicate where their custom entries are added, and will also gracefully remove them when plugin is disabled or removed.  Others may not be as graceful and may have a risk of damaging your .htaccess file so you should make sure this file like your normal WordPress blog files are backed up on a regular basis.


Example of good Begin/End Comment tags that keep plugin only writing to a specific area.

In normal cases you wouldn’t have to manually touch your .htaccess file but there may be some considerations where you may want to.  Here are some examples where you will want to update your .htaccess file manually so you don’t have to install a WordPress plugin.

Remove Directory Indexing

Directory Indexing is when a user hits your website and there is no index homepage (like index.php, index.html, index.htm…etc) you will see a list of files on the server instead of any specific page.  This is a security vulnerability if someone goes to your www.myblog.com/wp-content/plugins for example and they would see a list of your plugins you have installed for WordPress.  To prevent this from happening you simply add the following statement in your root .htaccess file (the one at the top folder in your wwwroot directory) or you can create a custom .htaccess file for each folder where you want to remove directory indexing.

In the .htaccess file just add:

#Remove Directory Indexing

Options All -Indexes

Once this is saved you won’t be able to see a list of directories or files in that folder anymore and this helps secure your wp-admin, wp-plugins and themes folder from being visible as well as any other custom folders you may have created.

Custom Redirects

Often referred to as URL masking or cloaking, it really isn’t it is just a redirect.  You basically usually want to take an affiliate link that you have and make it look like it is just going to your site.  You see this often, like going to www.myblog.com/go/myfavoriteproduct and it will redirect to myfavoriteproduct.com?youraffiliateID

This typically can be done with plugins and there are many good ones, but all plugins add some overhead with some more than others.  This overhead grows rapidly the more you have.  It is more efficient and faster if you build manual redirects in the htaccess file but this method works better if have very specific products and have a smaller number that you want to specifically target.  If you do multiple affiliate marketing to services like Amazon Affiliate or other programs where you are constantly grabbing links, images and want to link then a plugin may be easier and preferred.

To add a custom redirect to an .htaccess file simply open up the .htaccess file in your root web folder (the one above wp-content usually called wwwroot) and add the following line.

redirect /uri-prefix/uri-product http://www.youraffiliateproductlink.com

uri-prefix is some standardized virtual folder you want to use to indicate everything to this folder will be redirected, you need to make sure you don’t have a redirect possibly conflict with a blog post URI so you should put a pre-fix folder.  This could be something like /go, /recommend, /buy…etc.  Many use /go as the default.

uri-product is basically a short version of the product name like /go/greenipadcase if you were redirecting to your favorite green iPad case that you affiliate market for example.

You can have as many of these type of redirects as you want for example:image

Again, custom redirects in the .htaccess file will process faster and cause less overhead than using plugins but if you need dozens it is probably better to use a WordPress plugin.  You also lose all of these links if your .htaccess file is corrupted or replaced which is why it is important to backup the .htaccess file often.

WWW Redirect

One function of the .htaccess file is to ensure your website is loaded the same way and is indexed by Google as a single URL.  All websites might be able to be reached by both mywebsite.com and www.mywebsite.com but you will want to only allow 1 or the other.  Typically it is better to use the standardized www.mywebsite.com format for many reasons including thinking about users who may be behind proxy servers that may blog non www. based sites.

To do this most hosting providers have the ability to enable this feature for you with a click, but you can easily implement this yourself via a .htaccess file change to.

Just edit your root .htaccess file and add these statements

#Redirect all non-www to www.yoursite.com
RewriteCond %{HTTP_HOST} ^yoursite.com$
RewriteRule (.*)$ http://www.yoursite.com/$1

These statements say that anybody who hits your site as yoursite.com will automatically redirect to the same page on www.yoursite.com this also prevents Google from indexing 2 versions of your site and only indexes the www. version of your site.

Example of the lines in .htaccess file:image


Full Website Redirect

Let’s say you have a new website or moved from .org to .com or some other variation of your website, you will want to do a full website redirect so the homepage and every link from your original site redirects to your new site until Google picks up the updates and indexes the new site properly.  You will do this with 301 redirect statements in the Apache .htaccess file.

Replace your entire .htaccess file content on your original site (the one you want to redirect to new site) with these lines:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.yournewsite.com
RewriteRule (.*) http://www.yournewsite.com/$1

In Summary

These are just some of the lines and items that may lurk in your .htaccess file or types of statements you may want to add to both help secure your WordPress installation, make affiliate links more user friendly and easier to remember if you use them repeatedly and to make sure only 1 version of your site is accessible and indexed by Google.

Again, there are WordPress plugins that can do some of these things but minimizing the number of plugins you have installed can help keep your WordPress blog having better performance so I would minimize plugins unless you need them.  I do recommend you use W3 Total Cache as a caching plugin which adds many caching statements to your .htaccess file.

We are influencers and brand affiliates.  This post contains affiliate links, most which go to Amazon and are Geo-Affiliate links to nearest Amazon store.