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
↑ Back to top
- 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
Getting Acquainted With Your .htaccess File
↑ Back to top# 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
↑ Back to topRedirect 301 /oldpage.html http://www.yoursite.com/newpage.html
This code will send visitors accessing "oldpage.html" to "newpage.html" instead.
Redirect all files in a directory:
Redirect 301 /community http://www.mysite.com/
This code will direct visitors accessing any file in the directory, "community", to http://www.mysite.com/.
Redirect whole site:
Redirect 301 / http://www.new-site.com/
This code will direct all visitors to any page on your current website to http://www.new-site.com/.
Disable Hotlinking
↑ Back to top RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://YourDomain.com/images/nohotlinking.gif [NC,R,L]
Remove category From Permalinks
↑ Back to top RewriteEngine On
RewriteBase /
RewriteRule ^category/(.+)$ http://mydomain.com/$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
↑ Back to topErrorDocument 404 /
Redirect to another page (like your search page):
ErrorDocument 404 /search.php
Masking Affiliate Links
↑ Back to topRedirect 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 http://www.mysite.com/amazon/ASIN, with "ASIN" being the individual prouduct ID and "YourAmazonID" being your affiliate ID provided by Amazon.
RewriteRule ^amazon/(.*)$ http://www.amazon.com/exec/obidos/ASIN/$1/YourAmazonID [L]
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.
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
Hi!
Thank you for this article! One question – does the “Remove category From Permalinks” also remove the “product-category”-Slug from Permalinks?
BR
Maike
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.
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?
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 : http://www.domain.com/media/kks/albums/vaisnava-seva/
And i want to redirect it to:
http://www.domain.com/vaisnava-seva/
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?
Tom, nice write up. I am new to WP. I set up a default barebone site from WP and tried to do redirect from http://mydomain.com/publications to http://myseconddomain.com/publications and got “404 not found” error.
My .htaccess has the following:
Redirect 301 /publications http://myseconddomain.com/publications
I also tried: ErrorDocument 404 /publications http://myseconddomain.com/publications
What am I missing. I would appreciate your input.
Thank you in advance.
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).