How to create a vanity URL in WordPress with a simple page redirect

Hey guys, I’ve got a super simple WordPress tip for you today that has come in handy numerous times over the years. In this age of short URLs, there are often times when you’ll need a top-level, “pretty” URL on your main domain that points to a longer, more complex URL.

For example, we are about to start an affiliate campaign with WPEngine, and my affiliate linked looked like this: http://wpengine.com?a_aid=4e6fc813411ff, which isn’t very pretty. I’d much rather link to a nice looking URL such as http://androidandme.com/wpe, which will read better in banners, in videos, and when being said aloud.

To get this done, we’re just going to make a simple custom page template that uses a simple PHP command to tell the browser that the page has moved. Take the following code and save it inside your main template folder as page-redirect.php:

<?php
/*
Template Name: Redirect
*/

$redirect_url = get_post_meta($post->ID, 'redirect_url', true);
if ($redirect_url) {
Header( 'HTTP/1.1 301 Moved Permanently' ); 
Header( 'Location: '.$redirect_url.'' ); 
}
?> 

If you’ve made a custom page template before, that top part probably looks familiar to you. It’s the special bit of markup that lets WordPress know that you’ve defined a new page template, which will make our new template selectable from the ‘Edit Page’ screen in /wp-admin.

After that, we call up a custom field, “redirect_url”, which contains the long and ugly URL we want to point the browser to. Following the retrieval of that value, we use a PHP header function to send the user along to the new page.

Using your new custom template

To actually use this new custom template, there are just a few quick steps. When creating a new page, make sure that you select your custom template. There will be a drop-down in the right column where you can select your “Redirect” template (see image).

Also make sure that your set your page slug to something pretty. If you’re page is titled “Hey check out my new link”, your url slug will be hey-check-out-my-new-link. Don’t do that. Make the slug something simple, like “wpe”.

All that’s left now is to enter your long, ugly URL into a custom field labeled with the key of “redirect_url”. If you’ve never done it before, fear not, because it’s a much simpler process than people make it out to be. All you need to do is locate the “Custom Fields” box on the “Edit Page” screen and enter a key and value pair. The key is the title, “redirect_url”, and the value is the new URL, in this case: http://wpengine.com?a_aid=4e6fc813411ff

The great thing about this technique is it will work with any URL and doesn’t require a plugin. We use the technique lots of times internally, linking to our own specific posts with long URLs. As I mentioned above, it’s a great technique for video or a podcast, where you’ve actually got to say aloud Don’t forget to check out androidandme.com/wpe for a great deal on WordPress hosting.

DATE21st December, 2011

CATEGORIESposts

TAGS ,

4 Comments

  1. Do you think this is better than e.g. using the Redirection plugin? (perhaps a rhetorical question, since if you thought it wasn’t better you wouldn’t be doing it).

    The nice thing about a plugin like that is you get an organized view of all your redirects.

    • clark

      There’s a good chance the plugin does a much better job, I just like doing things from scratch when I can. I don’t particularly do this often, especially needing to track clickthroughs (if I do, the target link usually does so), so something lightweight was fine for me.

  2. This is awesome and quick… any ideas if I wanted to do a delay before the redirect to have analytics track conversion first before redirecting or would this register a conversion on analytics. Thoughts?

Leave a Reply