Plugins Minimalism: What to Use .htaccess to redirect URLs

Jun 2, 2023

While there's no reason to avoid applying a plugin, if it can save you the time of a person, thats often not the case. There's an enormous number of plugins available performing basic functions that are as easy to implement manually. For example like I said in my last post, there are several plugins to allow the tracking code that is used in Google Analytics. Rather than using a plugin, it is just as easy to paste the given code straight into your WordPress header file.

I think if more people recognized how easy it is to duplicate the function of certain plugins manually, they would think twice before installing the plugins. In doing so, we'd improve your security on our sites and the technical capability of the WordPress community in general.

In this regard, today I'm going to guide you through a step by step DIY guide to creating and managing URL redirects using nary a plugin in sight.

The Fundamentals of URL Redirection

redirection

There are several reasons why you might want to set up a redirected URL in WordPress. A few scenarios in which redirection could be useful can be:

  • Transferring a single document
  • The transfer of your site's domain to new domain
  • Naming the URL structure an internet page
  • Changes to the standard WordPress URL structure
  • Capturing errors 404
  • Disabling image hotlinking
  • Masking affiliate links

There are now some simple URL rewriting options available from within the standard WordPress dashboard, but the functionality that comes with it is restricted. If you want anything more than beautiful permalinks, you'll probably end up looking for an appropriate plugin.

Look up the WordPress.org directory for "redirection" and you'll get a few pages of plugins each of which could accomplish this job for you. Most popular is the plugin named Redirection, which offers a fairly comprehensive set of settings and options and also the basic URL redirect feature.

While an application like Redirection can be convenient and comes with some useful extra options, it's actually quite easy to duplicate the majority of its functionality using your own. A majority of users will utilize this tool to create basic redirects, and not make use of its advanced features. Additionally, you'll find that setting up redirection manually can be more flexible as compared to using a plugin after you get the hang of it.

Get to Know 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 open it, you'll need to utilize an FTP software to join the web server and go to the directory on the directory where you have installed WordPress. If you don't see the file, then you'll need to change the FTP client settings to show the hidden systems files.

Before making any edits to the .htaccess file, be sure to save a backup of the original. As innocuous as this little file appears, it's actually pretty powerful and can break your website or lock yourself out of your domain if you make an error. (But do not let this be a cause for concern). ;-))

Open the file it up with the text editor. There's probably something in the file in the past, something similar to this:

# BEGIN WordPress
RewriteEngine On
RewriteBase/
RewriteRule ^index\.php$ ([L])
RewriteCond %REQUEST_FILENAME !-f
RewriteCond: %REQUEST_FILENAME
RewriteRule . /index.php [L]
#"END WordPress

This is the base code installed by WordPress. In some instances, WordPress could not build or modify the .htaccess file because of an issue with file permissions in which case, if that is the case, the file could be empty or may not even exist. If there is no .htaccess file, you could make a brand new one in your text editor, save the file as "temp.htaccess" and alter the name using your FTP client once you have uploaded it.

Other than the above code (which is generated when WordPress is installed WordPress) There could exist other code within the field. Do not edit or delete anything unless you're aware of the procedure.

Also, make sure to edit the file only with a code editor or an ordinary text editor such as Notepad. If you view it using Word processing software such as Word, it may well introduce strange characters to the file which will stop it from working properly.

The updated code can simply be written or pasted beneath the code already in place. Once you're finished, upload the file, and you'll be good to go.

Now that you have your .htaccess file, let's start with redirections!

Basic Redirect 301

A redirect of 301 to direct traffic coming from a directory or entire site to a new address if you relocate your website or alter the filename.

 Redirect individual pages:

 Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html

This code redirects users who are navigating "oldpage.html" redirecting them to "newpage.html" in place of "oldpage.html.

 Redirect all files in the 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 the entire website:

 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/.

Deactivate Hotlinking

If a different website links directly to your pictures the bandwidth is being used (as well as being incredibly unprofessional). This code you could create a redirection that replaces hotlinked images with an alternative image that you like:

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

As a default, WordPress uses /category/ as a prefix in URLs before the title of every category. This isn't necessary. you can make smaller and more attractive URLs with this piece of code:

RewriteEngine On
RewriteBase /
 RewriteRule ^category/(.+)$ http://mydomain.com/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond: %REQUEST_FILENAME
RewriteCond%REQUEST_FILENAME!
RewriteRule . /index.php [L]

Redirect 404 Errors To Another Page

404-error

Rather 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 the homepage
ErrorDocument404 /

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

There are a variety of reasons you may want to cover affiliate links in your website. Apart from obscuring the actual URL, and concealing your affiliate ID, it can create links that are simpler, cleaner and less complicated to remember:

 Redirect 301 /recommends/funkyaffiliate http://youraffiliatelink

This is a great option for products that have only one affiliate link and can create a redirection for each product so you'll end up with an array of URLs all appearing to be within an area called "recommends" on your site (obviously you could name it any name you wish, however"recommends" is a good choice for affiliate links).

Another method to accomplish this can be useful in the case of affiliate links, where you may be linking to many different items on a single site and it's unwise to make a different line within 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]

WordPress Plugins: Very Handy, However, they are not always essential

If you're looking to do more with the .htaccess document, you'll discover many useful tricks that could be used on any site. Just by adding a couple of codes, you'll enhance security for your WordPress installation improve speed and performance of your site, block spammers, and much more.

Next time you want to enhance the functionality of your WordPress site, rather than going straight to the plugins directory, do the Google search. There's a chance that there's a simple way to achieve what you're looking for without requiring another plugin, and possibly placing your website in danger.

Are you utilizing one or all of the .htaccess hacks yourself? Are you feeling like you over-rely on plugins on your WordPress site? I'd love to hear your thoughts and experiences in the feedback.

Image credits: Colinbrough / sarej / 10 Minute Photoshop