Plugins Minimalism: How to Use .htaccess to Redirect URLs

Written by Tom Ewer on November 11, 2013 Blog.

I wrote in a previous article about the potential dangers of WordPress plugins and the fact that one should seek to minimize the number of plugins on a WordPress installation. While plugins can be incredibly useful and powerful, that same power is also the same thing that can make them potentially dangerous.

Although there’s no shame in using a plugin if it’s going to save hours of your time; thats often not the case. There are a great number of plugins available performing simple functions that are nearly as easy to implement manually. For example, as I mentioned in my previous post, there are several plugins available to insert tracking codes like those used in Google Analytics. Rather than using a plugin, it is just as easy to paste the provided code straight into your WordPress header file.

I think if more people realized just how easy it is to replicate the function of certain plugins manually, they would think twice before installing them. In doing so, we’d increase the security of our sites and the technical capabilities of the WordPress community as a whole.

With that aim in mind, today I’m going to go through a step-by-step DIY guide to creating and managing URL redirects with nary a plugin in sight.

The Basics of URL Redirection

redirectionThere are several reasons why you might want to set up a redirected URL in WordPress. Some possible situations where redirection can come in handy are:

  • Moving an individual file
  • Moving your site to a new domain
  • Renaming the URL structure of a page
  • Changing the standard WordPress URL structure
  • Capturing 404 errors
  • Disabling image hotlinking
  • Masking affiliate links

There are now some basic URL rewriting options available from within the standard WordPress dashboard, but the default functionality is pretty limited. For anything other than pretty permalinks, you’ll probably find yourself hunting for an appropriate plugin.

Search the directory for “redirection” and you’ll get a few pages of plugins, all of which can perform this task for you. The most popular is a plugin simply called Redirection, which offers a fairly comprehensive set of options and settings as well as the basic URL redirect functionality.

While a plugin like Redirection can be convenient and includes some handy extra features, it’s actually surprisingly simple to duplicate most of its functionality on your own. Most people will use this plugin just to do simple redirects and make no use of its advanced features. Moreover, you’ll probably find that setting up redirection manually is actually more flexible than using a plugin once you get the hang of it.

Getting Acquainted With Your .htaccess File

Used padlock green.The .htaccess file is a configuration file that you will find in the root folder of your WordPress installation. To access it, you’ll need to use an FTP client to connect to your web server and navigate to the directory in which you installed WordPress. If you don’t see the file, you may need to change your FTP client settings to show hidden system files.

Before making any edits to your .htaccess file, make sure to save a backup of the original. As innocuous as this little file may be, it’s actually pretty powerful and it’s quite possible to break your website or lock yourself out of your domain if you make a mistake. (But don’t let that worry you too much ;-))

Download the file and open it up with a text editor. You’ll probably have some code in there already — something like this:

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

This is the base code installed by WordPress. On some occasions WordPress may be unable to create or edit an .htaccess file due to a file permissions issue, and if this is the case, your file may be blank or not exist at all. If you have no .htaccess file, you can create a new file in your text editor, save it as something like “temp.htaccess” and change the name using your FTP client after you upload it.

Apart from the above code (which is generated when you install WordPress), there may be some other pre-existing code in the field. Don’t edit or delete anything unless you know what you’re doing.

You should also make sure to edit the file only with a code editor or plain text editor like Notepad. If you open it in a word processing package like Word, it may well insert strange characters into the file which will stop it from working properly.

The new code you include can simply be written or pasted into the file below the existing code. When you’re done, re-upload the file and you should be good to go.

Okay, now you’ve got your .htaccess file, let’s start making some redirections!

Basic 301 Redirect

You can use a 301 redirect to send traffic from a page, directory or entire site to a new address if you move your site or make filename changes.

Redirect individual pages:

Redirect 301 /oldpage.html

This code will send visitors accessing “oldpage.html” to “newpage.html” instead.

Redirect all files in a directory:

Redirect 301 /community

This code will direct visitors accessing any file in the directory, “community”, to

Redirect whole site:

Redirect 301 /

This code will direct all visitors to any page on your current website to

Disable Hotlinking

If another website is linking directly to your images, it uses your bandwidth (as well as being downright rude). With this piece of code you can create a redirection that replaces hotlinked images with an alternative image of your choice:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)? [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ [NC,R,L]

Remove category From Permalinks

By default WordPress includes /category/ in URLs before the name of each category. As this is unnecessary, you can create shorter and nicer looking permalinks with the following piece of code:

RewriteEngine On
RewriteBase /
RewriteRule ^category/(.+)$$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Redirect 404 Errors to Another Page

404-errorRather than displaying the default 404 error page when files aren’t found, you may want to direct the user to another page such as a search page or your homepage.

Redirect to homepage:
ErrorDocument 404 /

Redirect to another page (like your search page):
ErrorDocument 404 /search.php

Masking Affiliate Links

There are several reasons why you might want to cloak affiliate links on your site. Apart from obscuring the actual URL and hiding your affiliate ID, you can make your links cleaner, shorter and easier to remember:

Redirect 301 /recommends/funkyaffiliate http://youraffiliatelink

This works well for products where you only have one affiliate link and can set up a redirect for each product so you’ll end up with a set of URLs, all appearing to be within a directory called “recommends” on your site (obviously you can name this anything you like but /recommends/ works well for affiliate links).

The other method of doing this is helpful for affiliate links where you may be linking to many different products on one site and it would be unrealistic to create a new line in your .htaccess file for every single link. For example, rather than using the rather lengthy and unattractive affiliate URL provided by Amazon for individual products, you can replace it with something like, with “ASIN” being the individual prouduct ID and “YourAmazonID” being your affiliate ID provided by Amazon.

RewriteRule ^amazon/(.*)$$1/YourAmazonID [L]

WordPress Plugins: Handy But Not Always Essential

If you’re interested in doing more with your .htaccess file, you’ll find that there are loads of handy tricks that can be used on any site. By just adding a couple of lines of code you can improve the security of your WordPress installation, speed up and optimize your site, block spammers and more.

Next time you want to add some functionality to your WordPress site, instead of heading straight for the plugins directory, try doing a Google search. You may well find that there’s a simple way of achieving what you want without installing an additional plugin and potentially putting your site at risk.

Do you use any of these .htaccess hacks yourself? Do you feel like you over-rely on plugins on your WordPress site? I’d be interested in reading your thoughts and experiences in the comments.

Image credits: Colinbrough / sarej / 10 Minute Photoshop


9 Responses

  1. Syrehn
    November 11, 2013 at 7:56 pm #

    While I agree personally with what you’re saying in regards to manually coding something into the theme over using some plugins (for simple things) the truth is there are a great MANY users who are not savvy enough that it’s “just as easy to paste the provided code straight into your WordPress header file.” vs. using a well coded simple plugin.

    You also have to contend with the fact that if those same users modify a theme file and are not aware of child themes then they lose those modifications (seen that happen lots too). I have run into more than my fair share of “DIY” cases where had certain users used one of those simple plugins, instead of being under the impression that “more plugins are bad” they would have saved the cost of getting me involved when something went awry. Those are the same users that, while something might be simple to us, I would NEVER encourage to play around in their .htacess file.

    That being said, thanks for the rest of the article though, the insights on the .htaccess mods are appreciated.

  2. Brendyn
    November 11, 2013 at 8:13 pm #

    Anybody on managed hosting like WP Engine or using NGINX can’t use the .htacxess file at all so there’s a pretty strong case to use plugins like “redirection” in those use cases

  3. kermit73
    November 11, 2013 at 8:13 pm #


    Thank you for this article! One question – does the “Remove category From Permalinks” also remove the “product-category”-Slug from Permalinks?


  4. David Decker
    November 11, 2013 at 9:17 pm #

    While a lot of those googled snippets may work, a well coded, maintained plugin is better – especially for not so tech-savvy users!

    You’re telling, that plugins may be risky, your code snippet advise is often way riskier! Plugins could be updated – and will most of the time – I never saw any blog to update the code snippets to update for new WordPress functionality or to remove deprecated functions.

    I hate those posts “….without a plugin” — it leads (new) users on a risky road. Better is to educate users.

  5. SimbaHosting
    November 11, 2013 at 9:42 pm #

    I question the whole premise of this post. The idea is that it can be better to do something without a plugin, than with. The fewer plugins, the better!

    I’ve never seen a good argument for this. Much of WordPress internally is implemented using the same facilities that plugins can use. A properly-chosen plugin is likely to be better tested and debugged than an ad hoc solution. Again, if properly-chosen it’s also more likely to be cross-platform compatible (whereas .htaccess, for example, is more localised – it’s Apache-specific), and maintained with future WordPress versions. With an ad hoc solution you get to do all the work yourself. Why do this?

  6. Uddhava
    November 11, 2013 at 10:17 pm #

    This post pushes for simplicty, something that applies to lots of things, incl WP. And i also think that the less plugins the better (and faster). Sometimes it is worth to check out all functionaility from the plugins that you do have.

    For example, the SEO plugin from Yoast offers to strip the /category/ from permalinks. No messing with the .htaccess necessary.

    I like the redirect examples. But do give more example, like:

    I have this page :
    And i want to redirect it to:

  7. checkmate
    November 12, 2013 at 7:23 pm #

    how does Disable Hot linking work with things like Google images? will Google still be able to see those images? IF not, does this affect your seo?

  8. John
    November 15, 2013 at 2:10 am #

    Tom, nice write up. I am new to WP. I set up a default barebone site from WP and tried to do redirect from to and got “404 not found” error.

    My .htaccess has the following:
    Redirect 301 /publications

    I also tried: ErrorDocument 404 /publications

    What am I missing. I would appreciate your input.

    Thank you in advance.

  9. Phil T
    November 25, 2013 at 8:29 pm #

    Personally, I think that there *is* a place for articles like this & as an intro to htaccess modification I think Tom’s written it well.

    I agree that this might be beyond the average WordPress user’s comfort zone but unless we demonstrate to them that these things can be done how are they going to push beyond that comfort zone?

    Choice is a wonderful thing.

    That said, however, when I develop sites for clients I wouldn’t entertain such things as changing htaccess, as in this article, but instead choosing to use WordPress’ strengths and implement everything with plugins (preferring plugins over framework options) – that’s WordPress’ major strength, right? But when I’m building a site for myself, or just tinkering, I love nothing more than hacking the heck out of it all in the name of efficiency (and just because I can).