Bike Maps

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.

Leave a comment

Change of Direction!

Initially I had planned for this blog to strictly be a promotional tool for my other blog/webapp www.ridefreebikemaps.com. After making this blog and failing to update it all I decided to abandon that idea and turn this into a personal blog.

The initial idea was to make all kinds of posts optimized for the keywods ‘bike maps.’ Now I’m just gonna post about the things that I’m into, cycling, beer, tech, and whatever adventures I get into. I’ll be doing a lot of posting using posterous so if you prefer you can get the same quality content there!

So don’t expect too much from this blog. It is going to be my ‘fun only’ blog. If you are down to look at the random pics I take with my phone and various rants and ramblings great! Subscribe to the rss!

Until next time!

-Ian

Leave a comment

Google Bike Maps!

Well they havent come about yet but they will soon! Check out the Google maps trike in action:

[youtube=http://www.youtube.com/watch?v=sP6gCVRnTu8&hl=en_US&fs=1&]

Amazing! I can’t wait for Google to unveil their ‘bike there’ option. It is rumored that they will be doing it soon, what ever that means. I am especially excited because I have written a web app that formats Google maps to use in a bike map holder. My goal it to take free information from Google and using web 2.0 magic create some really useful tools. When they finally do release ‘bike there’ my web app will be able to chart the best route for cyclists.

As it stands my software is best when used with other bike maps.  I add more functionality to it all the time so check it out and leave me a comment!

Ride Free Bike Maps

Leave a comment