Skip to content
 

Creating a “GoTo” URL For Your WordPress-Powered Site

For one of my other sites, I’ll be doing some postal mailings in which I’ll need to include the URLs of some of the posts I’ve made. I really don’t want to force people to have to retype those horribly long URLs. I could use a service like TinyUrl.com, but I’m not happy giving a third party control of portions of my web site. So I’ve made it easier by using the power of apache’s rewrite rules with WordPress’ Post ID #. So instead of me having to mail out a URL like:

http://www.showbizradio.net/2008/01/10/community-theater-schedule-wallpaper/

I can include this one, which will redirect to the same post, and is much easier to type, or read over the phone:

http://www.showbizradio.net/goto/2133

To do this, create a new folder under your WordPress directory. You can call it anything you like, but shorter is better. I’ve called the directory goto, although go would also work well.

Inside that directory, create a file called .htaccess. The leading dot is important!

Put these lines in the the .htaccess file:

RewriteEngine On
RewriteRule ([0-9]+) /index.php?p=$1 [R=301,L]
RewriteRule (.*) / [R=301,L]

The first line simply enables the ability for the web server to process the request.

The second line says that if any page request in your “goto” directory is only digits, to pipe those digits into the index.php program. The R=301 tells web browsers and search engines to permanently redirect to the new url, and the L means this is the last command to execute.

The third line catches any other request (such as http://www.showbizradio.net/goto/heck) by simply redirecting any other request to your site’s home page.

And that’s all there is to it. Let me know if you have any problems with this. I’ve tested it only on WP 2.3.2, running under Apache. It should work fine if you have customized your site’s permalink structure.

20 Comments

  1. [...] Creating a “GoTo” URL For Your WordPress-Powered Site [...]

  2. hypotheek says:

    Should i have root permissions to make this work?
    I’m currently use hosting from a third party.

  3. You shouldn’t need to be root, but it is possible that .htaccess files in the directory may not work. It all depends on how your host has set up your server.

  4. Michael Locke says:

    Great post, thanks for the tip.

  5. Per Ove Mikkeldust says:

    Wow thanks for the tip. I will use it on my computer article blog. Thanks again.

  6. Binh says:

    Wow nice tip. Thanks. I might try this.

  7. estetik says:

    Great tip. Thank you for sharing the info.

  8. Thanks for the info. I also use WordPress, so this function may come in handy.

    I also do not feel comfortable giving away any control of my sites to a third party. Great info, thanks again.

  9. TheAntiHype says:

    I’m not a techie but could you achieve the same result by using a 301 redirect plugin? I have a plugin that allows a long ugly post URL to be pasted into the field and an automatic redirect to a new page is created. In your example you could just redirect to a new page you created with the title you want – eg showbizradio.net/promo

    Have I missed something?

    Mark Wilson

  10. @Mark, You could us a redirect plugin, but why load yet another plugin? Yes, a plugin would allow you to use an abbreviation of your choice for each post, but I assume then you’d need to edit each post to define a custom field. This system is fairly automatic, and simple. With my suggestion, you also don’t need to worry about naming conflicts.

  11. @Mark – Don’t use SpamArrest – you are causing headaches to people by forcing others to deal with your spam. I have unsubscribed you from having comments emailed to you.

    Several years ago, SpamArrest used the email addresses of people that sent messages to SpamArrest’s customers for the marketing of SpamArrest’s services. They may not do that anymore, but challenge response is still a bad way to deal with spam.

  12. Todd says:

    How is this any different than simply rewriting the htaccess by hand and creating a condition / response that numbers each post incrementally?

  13. @Todd, If you use a “goto” directory of some sort, you don’t need to tweak anything again, any value passed to that url will redirect automagically.

  14. trevor says:

    Micheal, this is brilliant, short and elegant. I love it.

    @hypotheek – One primary reason that it might not work is if your service provider was using the common apache config files to configure everything. I used to do this before I figured out that this was another way.

    When an isp or service provider is using something like plesk or another management tool, there is one apache directive file per domain instead of 50+ domains being managed by one file.

    The long and short of this, is that even though a few users have access to their own apache config files, ISP’s don’t want the headache of repairing them, so they allow .htaccess functionality to occur directly in the folder where something needs to be acted on, instead of the older method of embedding such functionality into a very long complicated apache config file or even the newer method of putting htaccess type functionality in the config files that can get messed up.

    Btw, the other less elegent way of hiding a long url is to allow the long url to exist but to use javascript to ensure that a user’s mouse over always shows **something else** ha ha. Used sometimes as a hack to protect clickbank affiliate account urls.

    Well Done, Michael, I am going to use this too. In truth, it looks so useful, i think i will try to memorize the lines.

  15. Hannah says:

    Hey, that’s a great tip, thanks for sharing!

  16. Ikki says:

    Smart move! Damn I was expecting something more difficult when I first read your post’s title hahaha

    This is one clever idea! Thanks for the tip!

  17. Jason says:

    brilliant and simple. thanks!

  18. Warren says:

    I was wondering how to achieve that — thank you!

  19. david miller says:

    That’s clever, I sometimes feel embarrassed to keepasking my devloper for help on my WP blog. Seems even easy enough for me!

  20. Zoltan says:

    Thanks for this, I have been using a plugin to achieve a redirect but this is a much cleaner approach. Really worry about using too many plugins for reason of speed and security. Many thanks for you help.