Skip to content
Nick Baker edited this page Jun 5, 2014 · 26 revisions

Working With Seo Redirects

The SEO Redirect tool is used primarily for SEO purposes, but can also be used to redirect your users from a bad url to a good url. You can define rules that when presented with a specific URI where a 404 would normally occur, a redirect will occur instead to a place you define. URI can accept simple wildcards or regular expressions. Each Seo Redirect has a priority and an is_active status that must be turned on for the redirect to work. The redirect tool will grant the user to define very simple or complex redirect rules based on the incoming request, priority gives the ability to grant situational redirects. Below are all the things you need to know in order to start using the Seo Redirect tool.

Important Note: The 301 tool only loads and executes if a 404 would otherwise occur (or you explicitly call $SeoAppError->catch404(); in your application). By setting up a rule, you are not saying that redirect will happen if that incoming URI exists in your application. If your application returns a rendered page without error, the SEO toolkit is never even loaded regardless of rules. This is important to understand. This tool is designed to help you handle errors in a sane, and helpful way for Google and your users.

Setup

update file app/Config/core.php with the following:

Configure::write('Exception', array(
	'handler' => 'SeoExceptionHandler::handle',
	'renderer' => 'ExceptionRenderer',
	'log' => true
));

update file app/Config/bootstrap.php with the following:

require_once(APP . 'Plugin' . DS . 'Seo' . DS . 'Lib' . DS . 'Error' . DS . 'SeoAppError.php');

Admin Panel

Browsing and Managing Redirects

http://www.example.com/admin/seo/seo_redirects

Creating a new Redirect

http://www.example.com/admin/seo/seo_redirects/add

URI

  • Incoming URI that a user is requesting that you want to redirect them away from using a 301. Either a relative link following domain (www.example.com) or a regular expression.
  • Regular expression are started and ended with # sign. Please refer to Advanced Redirects for details.

Redirect

  • The URL (relative to the domain preferably) you want the matching URI to redirect to with a 301. This can be an absolute path, relative, or a pattern match (Regular Expression URI only).
    • If running a callback specify {callback} somewhere in the redirect. {callback} will be replaced with the return value of the callback function defined lower

Priority

  • The priority to give this redirect rule. In the case of a conflict the redirect rule with LOWER priority number will take the match and run the redirect.
  • Example:
    /test.asp -> /first_link with Priority 1 Will Overrule /test.asp -> /other_link with Priority 2
    • Going to /test.asp will redirect the user to /first_link NOT /other_link

Active

  • Toggle the active/deactivate status of this rule.

Callback

  • Callback function to call with, Model::function syntax. The requested URI will be passed in as a parameter. The return value of the function will replace the {callback} text in the Redirect field. If false is returned by the callback function the 301 Redirect is haulted.

Types of Redirects

There are two different types of 301 Redirects, basic and advanced.

Basic Redirects

Basic redirects can be Many->One, or One->One redirects. They are simple to implement and understand what is going on. URI and redirects should be relative to the domain.

####One->One:

  • URI : /library
  • REDIRECT: /guides/common-questions
  • Result: http://www.example.com/library Redirects To http://www.example.com/guides/common-questions

Many->One: Working with Wildcards

  • URI: /website*
  • REDIRECT: /
  • Result: http://example.com/website OR http://example.com/websites-are-fun OR http://www.example.com/website/param/really/long/url Redirects To http://example.com
    • Notice the * denotes anything following /website will match this this redirect rule.

By specifying an * at the end of your URI you are allowing the redirect rule to match anything past the specified URI.

Advanced Redirects

Advanced redirects use regular expressions to see if a redirect matches and then runs a pattern match on the redirect. This allows for far more flexibility but can be a little hard to read/write. Advanced redirects are the only redirect that can handle Many->Many redirects.

All regular expression redirect rules need to start and end (not including optional conditions) with a # sign, otherwise it will be treated as a basic redirect rule.

Advisory Warning: Regular Expressions redirect rules are very powerful but can also be dangerous causing nasty redirect loops and other badness. Proceed with caution.

Note: All Advanced redirects must be approved by IT. Once an advanced redirect is created it is then sent to an IT for approval. Once approved the redirect will take affect.

####Many->Many: Working with Regular Expressions

  • URI: #(.*)\?from\=sb\-tracked\:(.*)#i

  • Redirect: $1

  • Result: This redirect rule will match any URI with ?form=sb-tracked: on the end of it and redirect it to the same url without ?form=sb-tracked: appended to the end of the URI.

    • Note: the i at the end of the regular expression signifies to be case insensitive.
    • Note: The $1 signifies whatever is found in the first set of () marks.
    • For each set of () in the URI regular expression you will have ${number} to represent what was found in that area to use in your redirect url.
    • In this example, $1 = the part of the URL before the ? and $2 = the value after “tracked:”
  • URI: #/article/([0-9a-zA-Z]*)#

  • Redirect: /guides/$1

  • Result: This redirect rule is basically an alias for /article/ to /guides/ that will maintain the rest of the URI untouched.

Callback Redirects

By defining a callback in the SEO Redirect you can do some very powerful things, most notably the ablity to do what .htaccess redirect can't, access your database to find the correct url instead of showing a 404.

A good example of this would be redirecting broken article urls with a id-slug combination.

Callback Senorio

We have an article URL of http://www.example.com/article/123-slug where 123 is the id, and slug is the slug. What we'd like to do is 301 redirect any broken link with the id 123 to the correct URL with the slug. We could use wild card rules for every article, or we can just write one rule with a callback to Article. Here's how:

  • URI: #/article/([0-9]*)(.*)#i
  • Redirect: /article/$1-{callback}
  • Callback: Article::callbackFindSlugById

In your article model app/models/article.php or app/Model/Article.php write your callback.

public function callbackFindSlugById($request){
  $string = preg_replace("/[^0-9\-]/","", $request);
  $id = array_shift(explode("-",$string));
  if($id){
    $this->id = $id;
    return $this->field('slug');
  } else {
    return false;
  }
}

Note: If the callback returns false, the Redirect will be haulted.

This callback redirect rule will redirect any broken article link to the correct id-slug syntax.

Seo Redirect Shell

You can also manage your redirects with the built in Seo.seo_redirects shell.

cake Seo.seo_redirects search <url or redirect> Quickly search for an existing redirect

cake Seo.seo_redirects add <url> <redirect> (priority:100) (callback:null) Add a new simple redirect

Examples:

  cake Seo.seo_redirects add '/mybad/path*' '/my-cleaned-up-path' 50
  cake Seo.seo_redirects add '/myother-bad/path*' '/my-cleaned-up-path' 60
  cake Seo.seo_redirects add '/my*' '/my-failover-path' 10
  cake Seo.seo_redirects add '#/some-old-route-(.*)#i' '/new-route-$1' 10
  cake Seo.seo_redirects add '#/(admin|moderator)/(.*)#i' '/$2?old-prefix=$1' 10

Resources

Here are some helpful resources to learn more about using regular expressions

Web tool for testing your regular expression