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.
Image may be NSFW.
Clik here to view.
Perhaps the most important aspect of redirecting your pages is maintaining your current Search Engine position, 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 can be found in your root directory (often /www or /public_html). You can open that file in any text editor to view the contents.
Default WordPress .htaccess File
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+ 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
Redirect 301
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.domain.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 on 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.
Adding WWW to WordPress URL
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 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 – Meta tags and PHP Headers to name a couple, but .htaccess continues to be among the best because you can maintain your Search Engine rankings.
This post is by Kurt Maine at The CSS Blog