Using .htaccess to Create 301 Redirects in WordPress

Creating a redirect on your WordPress site can seem a little intimidating if you aren’t familiar with Apache and Regular Expressions. It’s also important to add your redirects without breaking default WordPress functionality.

WordPress htaccess Redirect Rewrite Posts and Categories

Perhaps the most important aspect of redirecting your pages is maintaining your current Search Engine ranking, and to accomplish that you’re going to need to do do a 301 Redirect; 301 being the HTTP Status Code for “Moved Permanently”.

The .htaccess file is called a dotfile, because the first character is a dot. Dotfiles often lack a file extension, and are primarily used for configuration purposes. Your .htaccess file can be found the your root directory of your WordPress installation (often /www or /public_html). You can open that file in any text editor to view the contents.

Default .htaccess File for WordPress 3 and 4

If you mess things up, you can always revert back to the default WordPress .htaccess file by copying and pasting the code below – provided you hadn’t already modified it before this tutorial.

# Default WordPress 3.0+ and 4.0+ htacess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

301 Redirects

By far the easiest way to add a simple URL-to-URL redirect is by using redirect 301. The format of this simple Apache command is redirect 301 old new.

Redirecting WordPress Pages

redirect 301 /home.php /index.php
# Or
redirect 301 /home.php http://www.develohost.com/index.php

Redirecting WordPress Categories

redirect 301 /articles http://www.binvisions.com/tutorials

By redirecting entire categories or directories, you’re also re-directing requests to specific pages from one directory to another. For example, with the category redirect above, a user attempting to visit /articles/post.php would be redirected to /tutorials/post.php.

Mod_Rewrite

Mod_Rewrite is a bit more complicated, as it employs Regular Expressions to match and forward URLs.

Forcing all trafic from non-www URLs to www URLs

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

No matter how you use Mod_Rewrite, you must first turn the RewriteEngine On, as seen in the sample above.

Redirect File Types

Let’s say you’re in a situation where you upload reports in the form of Word documents, but want to start using PDFs instead. Once you’ve uploaded all of your PDF files using the same name as your Word documents (minus the file extension, of course) you can use the following code to foward all .doc files to the same location with a .pdf extension.

RewriteEngine on
RewriteBase /
RewriteRule (.*).doc$ /$1.pdf

Summary

There are several ways to go about redirecting one page to another in WordPress – Meta tags and PHP Headers to name a couple, but .htaccess continues to be among the best way because of the amount of control it gives you over your 301 redirects. More importantly, those 301 redirects inform search engines that the piece of content that you are referencing has moved permanently, allowing you to maintain your authoring your page(s) have built up in search engine rankings.

About: Kurt Maine

Kurt Maine is a Full-Stack Web Developer with a passion for frontend Node.js frameworks like Angular, and backend frameworks like Ruby on Rails, and the PHP framework Symfony.


Leave a Reply

Your email address will not be published. Required fields are marked *