Redirection

Jun 26, 2006 | Tags: , , , , , | Written by Administrator

Redirection is a WordPress plugin to manage 301 redirections, keep track of 404 errors, and generally tidy up any loose ends your site may have. This is particularly useful if you are migrating pages from an old website, or are changing the directory of your WordPress installation.

Features include:

  • Supports both WordPress-based and Apache-based redirections
  • 404 error monitoring - captures a log of 404 errors and allows you to easily map these to 301 redirects
  • RSS feed for 404 errors
  • Custom 'pass-through' redirections allowing you to pass a URL through to another page, file, or website.
  • Full logs for all redirected URLs
  • Create redirections based upon a URL, browser, referring site, or login status
  • Automatically add a 301 redirection when a post's URL changes
  • Full regular expression support
  • Fully localized
  • Export all redirections to CSV, XML, or Apache .htaccess files
  • Import Apache .htacces files

NOTE: Version 2 is a major rewrite. While the plugin tries its best to retain any existing Redirection configuration it is possible that something may not make it through the upgrade process. As with any upgrade you should make a backup of your database before installing the new version. Remember that I'm not accepting any responsibility for any data loss!

Version History

  • 2.0.8 - Re-fix log delete
  • 2.0.7 - Fix incorrect automatic redirection with static home pages
  • 2.0.6 - Support for wp-load.php
  • 2.0.5 - Fix #255
  • 2.0.4 - get_home_path seems not be available for some people

Installation

Installation is just like any WordPress plugin:

  • Download redirection.zip
  • Unzip
  • Upload to redirection directory to /wp-content/plugins on your server
  • Activate the plugin
  • Configure options from Manage ยป Redirection

Note that you must have a permalinks structure setup through WordPress.

You can find full details of installing a plugin on the plugin installation page.

Core Concepts

Redirection uses three core concepts:

  • Modules
  • Groups
  • Redirects

A module consists of many groups, and each group consists of many redirections. A module determines how the redirections will be used, and a group allows you to separate redirections into logical units. A redirection represents an action performed when a particular URL is accessed.

Don't worry, there's more to come!

Modules

At the most basic level, a module determines how redirections are implemented. There are three types of module:

  • WordPress
  • Apache
  • 404 errors

The WordPress module uses WordPress to implement redirections. This means it works for all permalink-enabled sites and so has the greatest compatibility.

The Apache module, as you would expect, uses Apache to implement redirections. This means that each redirection is written to an Apache .htaccess file, giving you better performance than the WordPress module, at the cost of less compatibility and less flexibility (some features, such as redirection statistics, are only available from the WordPress module).

Separate from these is the 404 error module. This uses Redirection to keep track of 404 errors.

Modules

Each module can be exported to CSV, XML, or an Apache .htaccess file. You can also view logs for each module via an RSS feed

WordPress Module

The WordPress module is the default module and provides the most features. The main disadvantage of this module is that each time a URL is redirected it requires WordPress to be loaded (contrast this to the Apache module where a redirection occurs before WordPress loads).

This module is configured as follows:

Wordpress Module

These options are detailed below:

  • Canonical - determine whether the www should be removed or added to your site URL.
  • Strip index - if enabled then any attempt to access a index.php, index.html, or index.asp file will cause an automatic redirection to the same URL, minus the index file.
  • Time Limit - sets a site-wide PHP timeout limit. Useful if something is timing out.
  • Error level - sets the PHP error reporting level. Useful for removing unwanted warnings, or for viewing hidden ones

Apache Module

Rather than using WordPress to provide redirections the Apache module writes data to an Apache .htaccess file. If you site supports these then this will give you the greatest performance, at the cost of losing a few items of functionality. For example, the Apache module provides no statistical information so it is not possible for the Redirection plugin to keep track of how many times a particular redirection has been used.

The module is configured as follows:

Apache Module

The options that differ from the WordPress module are:

  • Location - the location of the .htaccess file. The default will be your site directory
  • Ban IPs - prevents certain IPs from accessing your site
  • Allow IPs - allow certain IPs to access your site
  • Raw .htaccess - add custom rules
  • Site URL - an advanced option which allows you to set the site base URL

Note that when writing to a .htaccess file the Apache module will retain any existing non-WordPress data, so you can still add custom rules.

404 Module

This module is a little different to the others in that it is designed to record 404 errors. Rather than make this a general function, as in previous versions, the power of this module comes from the fact that redirections created inside it will be ignored from the 404 log. In other words, the module records all 404 errors except the specified URLs. Combined with the RSS feed this gives you a lot of options to keep track of errors on your site.

404 Errors

If you do want to ignore a 404 error (for example, you site may not have a favicon.ico and you don't want this recorded) then you should create redirections as normal, but set the action to 'Do nothing'. As for other modules you can create redirections based upon any action or rule, or matching a particular regular expression.

Groups

A group is a logical collection of redirected URLs. You can create as many groups as you wish, and groups can be re-ordered. URLs are matched based not only on the order in which they occur within a group, but the order in which groups occur within a module.

Group Edit

As well as allowing you to collect redirections together, you can also specify whether the redirections within a group are logged.

As an additional configuration option you can select which group an automatically generated URL is put in when a post/page or category is changed.

Using Redirection

The main idea behind Redirection is that you create several URLs that you want to redirect. These URLs are placed within a group, which in turn is placed within a module. The module determines how the URLs are redirected, and the group allows you to logically organise the URLs.

Each URL (or redirection) can be configured to behave in different ways. When creating a redirection there are several pieces of information you must provide:

  • Source URL - This is the original URL that you want to redirect somewhere else
  • Match - This tells the plugin how you want to match the source URL
  • Action - Determines what happens if the URL is matched
  • Regular expression - Tells the plugin that the source URL is a regular expression (i.e. a pattern that may match many URLs)
  • Target URL - most actions have a target URL. This is typically the URL that the user will be redirected to should the pattern match

Create

Note that items can be re-ordered and this may influence which redirection takes effect.

Matching URLs

Matching a URL is a key part of Redirection and consists of a source URL. This URL must exactly match a URL that you want to redirect. For example, your site has the page:

http://yoursite.com/oldpage/that/needs/redirecting/

The source URL for this is: /oldpage/that/needs/redirecting/. The source does not require your website address, and it is only possible to redirect a URL that exists on your website (you cannot redirect an external website, for example).

Advanced users can make use of regular expressions to reduce the number of redirections they need to create. A regular expression is basically a pattern that tells the plugin how to match. For example:

/(\d*)/(\d*)/(.*)

This pattern tells the plugin that you want to match a URL that looks like:

/2007/05/some-url/

That is, the (\d*) indicates a number, and the (.*) a sequence of characters. Regular expressions are a complicated subject and this page will not attempt to give more than a passing overview of using them. If you do need more help then you should take a look at a regular expression website.

Remember that if the source URL is a regular expression then you must enable the regular expression option, otherwise Redirection will just treat you source URL as plain text.

In addition to the source URL you can also specify a match condition:

  • URL only - Only matches the URL (the majority of your redirections will use this)
  • URL and referrer - Matches a URL when the source and referring site matches (i.e. match the URL only when the user came from a certain website)
  • URL and login status - Matches a URL when the source and user's login status matches (i.e. match the URL only when the user is logged in)
  • URL and user agent - Matches a URL when the source and user's browser matches (i.e. when the user is using a particular type of web browser)

These special rules can be both positive and negative. For example, you can match a URL when the user is logged in (and be redirect to one URL), or when the user is not logged in (and be redirected to another URL). This makes it very easy to create custom rules where users are redirected if they are using a particular browser, or if they came from a particular website.

Actions

An action tells Redirection what to do when a source URL is matched:

  • Redirect to URL - The default case and the majority of your redirections will use this
  • Redirect to random post - An esoteric action that may be useful to some people
  • Pass through - An advanced option that allows you to masquerade one URL as another (i.e. when the source URL is accessed it actually displays the contents of another URL without the user being aware)
  • Error (404) - Causes a 404 error to be returned
  • Do nothing - A dummy option that can be used if you just want to track accesses to a URL

Configuration of rules and actions

Depending on the particular combination of action and match rule you may be required to provide further details for a redirection.

Basic URL redirection

You can specify which HTTP code is used to redirect a URL (301, 302, or 307):

Redirect Url

A couple of examples:

  /blog/(.*) => /$1

This will match any URL that starts with /blog/, and will redirect it to the same URL but without /blog/. For example, /blog/2006/10/01/mypost will be redirected to /2006/10/01/mypost.

  /2006/month_d+/(.*) => /2006/$1/$2

This will match any URL that starts /2006/month_, and is then followed by a number. This will be redirected to the same URL, but without month_. For example, /2006/month_1/something will be redirected to /2006/1/something.

To replace a single dash in a URL with a space:

  /tags/(.*?)-(.*?) => /tags/$1%20$2

Redirect to URL by matching user agent

This allows you to configure a redirection to occur when a specific browser (the user agent) is used:

Redirect User Agent

A set of pre-defined user agents is available from the drop-down menu or you can specify your own. The user agent match is always performed using a regular expression.

Two target URLs can be specified, one for if the user agent does match, and one for if it doesn't match.

Redirect to URL by matching referrer

Similar to the user agent rule but this one looks at the referrer. That is, if a user follows a link from another site to yours, the original site is passed along by the browser (unless disabled) to your site so that you know where the user came from. Using this rule you can base your redirections upon this original site.

Redirect to URL by login status

This rule allows you to match a URL based upon the user's WordPress login status. That is, if they are logged into your site.

Support

Please direct all support questions to the Redirection support forum. Any support questions left on this page may not be answered.

Bugs & New Features

A full list of all bugs can be found in the Redirection issue tracker.

A full list of all requested features can be found in the Redirection feature tracker.

Share This

Comments (page 2 of 45)

  1. author
    Andrey :

    Nov 16, 2006 3:42 pm

    You are my hero! - this is exactly how I imagined such a plugin in my wildest dreams.

    I am your hero too: found and fixed a bug:
    in redirection_item.php and in redirection.php (and maybe elsewhere?) you need to replace bloginfo ('home') with bloginfo ('wpurl') to make it work for people who installed wordpress into a directory but make it appear at their document root.

    ALSO: do you have an email list (or a feed) for just product update announcements? [-your blog is fun but I am suffering from information overload :-(

  2. oXfoZ :

    Nov 11, 2006 6:19 am

    Hello,

    First of all, thanks a lot for this very efficient plugin :-) Nevertheless, I had to modify redirection.php to make it work fine with my installation:
    483: if ($newurl != $url && $newurl == preg_replace ('@'.$pattern.'@', $item->url_new, $newurl))
    change to:
    if ($newurl != $url && $url == $pattern)
    This checks that the page has to be redirected

    505: header ("HTTP/1.1 $type".$types[$type]);
    506: header ("Location: $newurl");
    507: exit ();

    change to:
    header ("HTTP/1.1 $type ". $types[$type]);
    header ("Location: $newurl");
    header("Status: $type ". $types[$type]);
    exit();

    To avoid errors from Apache...

    Questions:
    To redirect not only 404 pages but also others, I had to withdraw this test: 460: if (is_404 ())
    Do you have any other solution?!

    Thanks by advance!

  3. Fra :

    Nov 3, 2006 6:31 am

    297: if($item){
    298:= foreach ($items AS $key => $item)
    299:= $items[$key]->id = $x ;
    300: }

    :P

  4. Jonas Smith :

    Oct 19, 2006 6:51 am

    Hi,

    The plugin seemed to work - until I changes my WP directory (which was why I thought I needed redirection in the first place). Now, when I activate the plugin (without touching any options) and request my frontpage, the browser goes "Problem loading page - Flock has detected that the server is redirecting the request for this address in a way that will never complete."

    Any ideas?
    Thx.

  5. John (author) :

    Oct 4, 2006 2:46 am

    Small update in 1.3 to stop draft posts getting picked up

  6. John (author) :

    Sep 27, 2006 4:50 am

    Mike: Good idea, I'll implement that one.

    Rob: Regex is a BIG topic so I can only scratch the surface. For the example you gave you could try something like:

    /weblog/(\d )/(\d )/index.php => $1/$2

    This is matching anything that starts with '/weblog/', ends with 'index.php', and has two groups of digits. \d represents a digit, the means '1 or more', and the brackets indicate that the values should be captured and can then be used in the replacement string ($1 is the first set of digits, and $2 is the second).

    Note that I haven't tested this regex pattern so it may not work exactly as you want.

    To answer your questions

    1. The plugin works because WordPress 2 has a generic .htaccess rule which pushes every URL access through WordPress. This means that a plugin can hook into the URL translation stage before WordPress takes over.

    2. I think something like that won't be possible or, at the very least, will be a very complex regex pattern

  7. author
    Rob :

    Sep 22, 2006 3:05 am

    I'm moving by Movable Type blog to WordPress, changing the URL form - and so, location - of all 360 of my posts (from the form http://www.domain.com/weblog/2005/12/audience_relati.php to http://www.domain.com/2005/12/audience-relationships-threat-or-opportunity/).

    I had hoped to set up individual redirects for each post, but that would be a nightmare, so I deployed your Redirection plugin, using a regex filter, to at least refer people from a dated post like http://www.domain.com/weblog/2005/12/audience_relati.php to the new monthly archive page of http://www.domain.com/2005/12/ (from weblog/(.*)/(.*)/index.php to /$1/$2).

    This was when I discovered the Related Posts for Your 404 plugin (www.w-a-s-a-b-i.com/archives/2006/02/20/related-posts-for-your-404/), which is almost exactly what I've been looking for - it applied related posts functionality (based on the slug) to your 404 page - ideal..

    But I hit a snag because these two plugins appear to overlap. That is, when a user comes to the former URL http://www.domain.com/weblog/2005/12/audience_relati.php, they are redirected to http://www.domain.com/weblog/2005/12/ (which doesn't exist; should be http://www.domain.com/2005/12/) - and the 404 template kicks in and searches for likely posts... not the desired effect.

    Do you have any ideas please?
    Thanks

  8. author
    Rob :

    Sep 20, 2006 9:05 am

    Please can you provide some examples of how to use the regex functionality?
    For example, how would I point people heading for /weblog/2005/12/index.php to /2005/12/? That is, people heading for /weblog/NNNN/BB/index.php to /NNNN/BB/?

    Two questions:-
    1. If the method with the plugin is separate from the .htaccess method, how does it work? And should I delete my .htaccess 301 redirects?
    2. I am migrating my Movable Type blog, with 360 posts, to WordPress, but changing the url structure (from /weblog/2005/12/audience_relati.php, whereby MT takes the first 15 characters of a post title, to /2005/12/15/audience-relationships-threat-or-opportunity/). I had started using .htaccess to set up redirects individually, but I have hundreds of posts - is it possible to enlist this plugin to intitiate some regex-based (?) method of automatic redirection? Ideally, for posts; if not, then, as above, for monthly archives.

    Thanks

  9. author
    Mike :

    Aug 28, 2006 6:01 am

    John,

    The plugin works great. One tiny issue I have found is with the automatic 301 creation. If I have a post in draft state and then publish it using a different title and slug, Redirection will create a 301 redirect entry.

    I don't know if it is possible to detect, but I think that the 301 entries should only be created when a published post's slug is changed. This way you don't end of with extra unnecessary rules.

    Mike

  10. johnnoone :

    Aug 11, 2006 11:42 pm

    Nice plugin. It works well on my blog.

Leave a comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Home | Software | Terms & Conditions | Sitemap | John Godley © 2008
Close
E-mail It