Feb. 13, 2011, 3:24 p.m.

Selectively Disable WordPress Canonical URL and Redirect

Recently I wanted to change the way Ride Free Bike Maps accesses the individual routes. The way I had set up the app worked with query strings for access. That made for an ugly url and it wasn't something the Google Bots would pay attention to. I tried to override their default behavior in the Google Webmasters page but it didn't work. I had run across a snippets about writing url redirects using .htaccess. I never really cared too much until recently because I'm lacking on inspiration for more updates to add. I started with an .htaccess tutorial. There are a lot of them out there and they all produce the same end result so I won't bother digging up the link. Basically the idea is to redirect the the users request url to a script. The script then dissects the url or gets a query string from the redirect and outputs the page you want. Sounds straight forward right?

Well with WordPress it isn't. The problem stems from WordPress' canonical urls. The canonical urls exist to redirect users and search engines from alternate ways of accessing the content to the preferred url. For the vas majority of WordPress sites this is the way to go. The alternate access points for the content are query strings which are ugly and bad for SEO. However, if you are running an app on your site which generates dynamic content then you will need to at least partially disable this feature. The first piece of the puzzle is to disable the automatic redirects when you access a non-canonical url. This is the most obvious part of the problem. I found a quick and easy solution from Mark Jaquith summarized on mydigitallife.com. Basically all you have to do is write (copy/paste) a simple plugin that disables the canonical redirect. Since I only need to disable the redirect on the route pages I made a modification with a fancy if statement:

/* Plugin Name: Disable Specific Canonical URL Redirection Description: Disables the "Canonical URL Redirect" features of WordPress 2.3 and above on specific URLs. Version: 1.0 Author: Ian Harper Author URI: http://ianbharper.com/ */ function map_page () { include(TEMPLATEPATH . '/page.php'); exit; } $routesResult = strchr($_SERVER['REQUEST_URI'],"/url_fragment/"); if ($routesResult) { remove_filter('template_redirect', 'redirect_canonical'); remove_action('wp_head', 'rel_canonical'); $_SERVER['REQUEST_URI'] = "/script_location"; } ?>

To use this plugin you need to replace url_fragment with a part of the url that you want to disable the redirect on. For my site route urls are formatted as /route/xx where xx is the route id. the script_location is the page that the url would redirect to. This is necessary because WordPress determines what page you are on by the $_SERVER['REQUEST_URI'] variable. Without this a 404 page is displayed no matter how much you curse at your monitor. To use this plugin all you have to do is pase it into your favorite text editor and upload it to your plugin directory on your site. The action from this script comes from the remove_filter and remove_action functions. The first stops the automatic redirect of the browser and the second removes the meta element in the head section. I learned about the meta tag by digging through the html source. Not many people worry about this problem so finding the solution was tough but PixelPunk had a solution.

The last thing I had to fix had to do with the way my Facebook plugin wrote the url for the page. I'm using the Simple Facebook Connect plugin which handles all the Facebook integration of my site. I was concerned with the like and share modules of the plugin. They use the WordPress get_permalink() function to determine the url to like/share. I replaced that with an if statement like the one above which changes the the link to the $_SERVER['REDIRECT_URL'] variable. This gives the desired url for the page. To edit the plugins use your favorite text editor.

Bikes

Programing Seo Wordpress