WordPress Page Redirect template

The very nature of WordPress demands that you work within it's infrastructure in order to maintain flexibility. Sometimes, though, the structure of your pages demand that you need to operate outside of this constraint.

This template lets you specify a single URL as the page content, then as the page loads, the template automatically redirects the page to this new location.

Usage

Once you've uploaded the template-redirect.php file to your themes… sub-directory, i.e. wp-content/themes/theme_name/

1 – Create a new Page in your WordPress control panel

2 – In the Page Content panel, just enter the URL (or local path) you want to redirect to:

3 – Set the Page Template dropdown to "Page Redirect":

That's it!

Note that in WordPress 2.7 and later, you will need to do a little additional setup work to display the page template dropdown (don't ask me why).

See this link for further details.

Examples

Examples of URLS might be:

  • category/flowers (a relative link to the permalink for "flowers")
  • ../page.html (a page outside of a WordPress subfolder (the WP folder being /blog/) but still in your site)
  • http://davestewart.co.uk/resources/ (a page on a completely different site)

The "Latest developments…" page in my sidebar automatically redirects to /category/development/ – or in other words, the archive for my "Development" category, as my home page is not the standard "last-post first" format.

Download

template-redirect.zip

74 Comments

  1. OMG You are a lifesaver. I've litterally spent 20 or 30 hours trying to figure out ways to do this. I even starting learning PHP. I am NO programer just a basic techy. Might site is not ready yet because I was waiting to figure this out. If you find out a non-hacked way to do this PLEASE let me know. Now I have to get on design and layout, not to mention other stuff.

    Again, THANK YOU!

    Alexander

  2. Dave Stewart

    Hey Alexander,

    I'm glad it's working for you!

    I'm not sure what you mean by "non-hacked" though? If you mean you want a solution that works solely within the core WordPress functions, then I don't know, to be honest. As far as I can tell in my limited usage of WordPress (ie, this blog) and the woefully-incomplete help, "page"s and "post"s are just database entries – that's just how WordPress works, so the management of static content falls outside this paradigm (and thus "hacks" like this).

  3. Came across this searching Google for a method to redirect pages to categories 🙂
    I guess this is the best way ever to do it. Thanks a ton, Dave. And a nice blog 😉

  4. Geoff

    nice clean solution…using pure Javascript in the source of the page has a visible "hiccup". thx !

  5. Thanks greatly for this!!! This is exactly what I have been looking for. One more obstacle on my list taken care of by the wonderful wordpress community. Kudos.

  6. Dave Stewart

    Thanks for the thumbs-up, chaps!

  7. I would just like to say… Thank you. 🙂

  8. Hey,

    One small problem – after installing the template and setting up two redirects, my site no longer gives out "404 – Not found" errors. All it does when someone types a wrong URL is point them to the first redirect I set up. You can try and see for yourself.

    Any idea why this could be?

    Thanks,
    Sebastian

  9. Dave Stewart

    Hi Sebastian,
    Well that's strange, because all the page does is grab the text from the post content, and redirect the page to the new location; it's really very uncomplicated, so I don't really see how it could affect your 404 page.
    What happens if you remove the template links?

  10. james

    doesn't work. i followed the directions–created a new Page, included the redirect URL as the only content, Saved and Published.

    i'm trying to redirect incoming traffic from a page that was deleted to my main page, by the way. so that when they click a link, they bypass the error result from a deleted page and go right to the Home page.

    cheers,
    -james

  11. Dave Stewart

    Hi James,
    What was the exact URL you placed in the content area?

  12. james

    http://mydomain.com

    just like that (with "mydomain" replaced with a real one, of course.)

  13. Dave Stewart

    Hmm. Not sure why it's not working for you, as it does here. To test, I've added a new page to my top links, "Dave Stewart", and that's just "http://davestewart.co.uk", and it works fine. Check it out, as I'll delete it in a day or so.

    Another option you could use would be to add an Apache Redirect Directive to an .htaccess file in your root directory, which would redirect traffic at the server level, rather than the page level: http://base64.co.uk/apache-redirect-directive/

    And if you want to email me, feel free.

  14. ethan

    Neat trick. My only suggestion is that $HTTP_SERVER_VARS is, I believe, deprecated in PHP 5. People should be using $_SERVER. I made this modification and it works fine.

    Thanks, though. This is way simpler than the Redirect plugin I found.

  15. Dave Stewart

    Cool. Thanks Ethan – I've updated it now 🙂

  16. hi. it works great, 1 question from me:

    Is it possible to redirect the page to an alternative site IN a new windows?

    1000 thanks

  17. looking for solution that i type my url and is redirected to new static page

    and how it can be done from the post…. is their anyway to do from post

    thankyou and looking forward for your prompt reply

  18. Dave Stewart

    Hi Murtaza,
    Sorry – I can't help you there! Page Redirect does one thing and one thing only.
    Cheers, Dave

  19. Here is an even simpler solution. If your permalink options are not set to the default option, just make the permalink the link that you want the page to be redirected to. I don't know how deep you can go but it's worth a shot.

  20. Awesome. Thankyou very much

  21. I think (just for searchbots happiness) it would be better add a 301 header before redirect:

    // do the link
    —–> Header("HTTP/1.1 301 Moved Permanently" );
    Header("Location: $link");
    die(");

    thanks a lot for your page-redirect-template. It solved my need in just one second!

  22. After trying two plug-ins that seemed either buggy or complete overkill I stumbled upon this page and find a 1-minute fix for my problem. Brilliant – thanks!

  23. Your plugin is working great, but there's one problem I've come across when trying to use it.

    We're using the Post Template Plugin, and I'm trying to point your plugin to redirect to a specific plugin we've made. This is the URL for the specific template:
    http://thelocallanding.net/wp-admin/edit.php?page=post-template/includes/manage-templates-page.php&action=create-post&template_id=6

    I've put that in the page to be redirected and set everything else accordingly (works fine with simpler links), but it seems that it will only redirect to
    http://thelocallanding.net/wp-admin/edit.php?page=post-template/includes/manage-templates-page.php

    leaving off the "&action=create-post&template_id=6". This shows a list of all templates we've made with the Template Plugin, but I cannot for the life of me convince it to keep that vital last part of the URL which would lead directly to the specific template I want to redirect to.

    I don't know much about this type of thing, but if you can think of any way to persuade your plugin to keep the full url, I'm sure world peace wouldn't be far behind!

    Thanks for the plugin, and any help you can give on this issue. 🙂

  24. Add another thanks to the pile! 😀 This is perfect – and no hiccups!

  25. Jim

    I'm trying to use it for WP 2.7 and it doesn't appear to be working. There is no section that allows me to choose page redirect on the "page" page.

  26. Thanks!!!

    Thanks! You just made my day 😀

  27. Melanie

    Hmmm…is there more to it? I uploaded the php file to my themes directory. But when I go to the template drop-down, the redirect option is not there. What can I do? I tried also uploading the redirect folder to my themes directory, with no luck. How'd everyone else get it to work? Are there limitations depending on which version of WP you have?

  28. Great!
    It also works with multiple languages (polyglot plugins)

  29. Ipstenu

    DUDE! This rocks! Thank you so very, very much!

  30. All I can say is great tip… It took me 5 hours to discuss with my host how to redirect one of my pages to my main site and he finally advised me to search google.. LOL.. Now I know my host still did a good job by giving me suggestion…

  31. This is what I've been looking for thank you, because before I have to link within my page which is very inconvenient, the user has to click two times…thanks a lot!

  32. Thomas

    This works great, thats for the help. Is there a way i can make this load a page in a new window rather than in the same window? Im a bit of a newb here and this script has helped a load. Thanks again.

  33. This is why I always found geeks to be so sexy! Always love to see some brains! Big muaaahhh and thank you for the great solution!

  34. Dun

    Thank you. I have been trying to figure this out for a while now.

  35. Dave Stewart

    Sorry to anyone who's left comments asking for help over the past VERY long while, I'm not really giving the blog much attention these days. I hope you all worked out your issues 🙂

  36. thanks for the plugin Dave, for anyone that might have making this work, the key is to installing the template page redirect pho file is to put it in the folder of you actual theme rather than your /theme root.

    For example if you are using a theme named spacemonkey, you should probably put your theme in a folder like /wordpress/wp-content/themes/spacemonkey.2.2/spacemonkey
    where all the other .php files are.

    Hopefully this will same someone a few minutes of their time.

  37. Dave Stewart

    Fair point Mike. I've updated the main post.

  38. pesky

    Hi there – thanks so much for such an elegant solution!!

    one question – if i have adsense on my pages (i do), will this get me in trouble with them? adsense requires a certain amount of text on my pages as per their terms of service, and if it isn't there it can get you knocked off their program.

    i know this works for humans, but will a bot 'read' the page as empty, besides a URL?

    i guess i could write any old rubbish after the url (?), but i thought i'd check with the geniuses here

  39. rendy

    thanks you very much..

  40. It's spooky how clreev some ppl are. Thanks!

  41. Yuri

    Hi Dave,

    Great stuff!

    Question: how can insert my Google Analytics (or any other tracking) code into this page? Will inserting "get_header();" as the first line of code do the trick (my GA code is in the header)?

    Thanks!

    Yuri

  42. Works great, unlike so many others I've seen on the web!

    Check it out in action – http://japantravelmate.com/ – the very top leaderboard banner has a URL "http://japantravelmate.com/japan-rail-pass/", however using the method from this post, redirects using my affiliate link!

    I'm a very happy webmaster, thanks!

  43. Hi Dave, I think this is what I need to create a button on my WP page that will take the reader to my WP blog?

    I have ftp access but I don't know how to upload to it. I can't just drag the php thing to the page.
    So I get stalled on this first thing:
    Once you’ve uploaded the template-redirect.php file to your themes… sub-directory, i.e. wp-content/themes/theme_name/

    Hope I'm missing something simple!
    Thanks, regards,
    Lucien

  44. OK, I found it. But it failed to install it because it was missing a style.css thing. Might your handy thing not work anymore?
    Thanks,
    Lucien

  45. James

    Thanks for this great script! However, it doesn't work if you set the URL to https. The redirection URL gets appended to the source page.

    I had to edit the template php so it looked for https instead of http, towards the bottom.

  46. dk

    Thank you very much. Really nice fix for sending to the first child of a page.

  47. Brilliant!

    I never comment on blogs but I had too just because this is such a time saver.

    Thanks

  48. Is there a way to use this redirect with a time delay?

    I need for my users to be able to view the wordpress page for a few seconds, then be redirected to another page.

    Can you give me the code to add to your template-redirect.php to do this?

  49. Fiona

    I know it's been about two years since the last comment was posted, but I thought I'd try anyway.

    First off, thank you! This saved me a huge head ache!

    Second… Is there a way to make the redirect remain on the current page, but just go down to an anchor tag? I'd like it to just redirect to the footer. Currently, it redirects to the home page, then the footer.

    If this isn't possible, that's okay. Going back to the home page isn't bad.

    Thanks!

  50. Dave,

    I've been looking for a solution like this for a long time. As a blog newbie, this is a lifesaver! I can't believe how easy it was to integrate into my wp theme. Many thanks.

  51. Rafael

    Excellent, thanks for the input. Works correctly in WordPress 3.5.2.

  52. I'm trying to link to an outside domain… This fix reads like it's what I'm looking for… but when I publish it; the URL string is added to the domain/subfolder that the page resides in… I get redirected to :http://ceracom.com/wp_ceracom/https://www.cera.net/portal/cart.php?gid=13

    instead of just the catalog.

    I've tried ../../ to escape up. but that didn't work either.

    Anyone else have this problem?

  53. I upgraded to the newest version of wordpress, and this template disappeared. I tried re-installing it, but got this error message:

    The package could not be installed. The theme is missing the style.css stylesheet.

    Any suggestions?

    • Dave Stewart

      Hey Paul,

      Sorry, have not looked at WordPress or this plugin in quite a while.

      Did you get it sorted?

  54. Victor

    Not working for me. Latest wordpress version

  55. Hi, this works nicely, there's one issue though. The page using this template still exists, you can get there by typing .php after it.

    Let's say I created "/work" page using this redirect template, which redirects to "/work/recent". It redirects if I go to "/work", but if I go to "/work.php", it'll go to the actual page and not redirect. Is there a fix for this?

  56. Nice post, Dave. Came up on Google Alerts – great to have found you!

    OptimizePress has a redirect template as standard – might be worth a look if you're running into any problems.

  57. Marc C

    What a fantastic method! I shall be using this technique going forward. Thank you Dave for sharing!

  58. Andras

    Let me add my thanks to the list!!! Great stuff.

    Based on this I might try to develop a plugin that uses custom fields.

  59. Tim

    Hi Dave,

    This looks like a fantastic solution!

    I may be in the wrong here because I am a complete novice, but when uploading the redirect theme it said "The theme is missing the style.css stylesheet" and consequently failed to install.

    Any advice?

    • Dave Stewart

      Hi Tim,

      The template itself doesn't reply on any stylesheets, so I suspect this is just a problem relating to your theme.

      Does that sound about right!?

  60. Mike

    Hi Dave

    I just tried this with WP 4.2.2. and found that the redirect template does not come up in the template dropdown on the new page page…..

    Are you able to help here?

    • Dave Stewart

      Haven't looked at this code for a long time, sorry! It's not that complex though, so crack it open yourself if you're handy with code and take a look.

  61. Dave, that's a terrific addition – works like a charm! (WordPress 4.2.2) Site that I listed in reply info is a client site that has had some major updates recently and for which I needed to change some page addresses for better SEO. Hence the need for a redirect. Many thanks!

  62. Pia

    This was a lifesaver! Pure awesomeness!

  63. Brilliant! Thanks so much, Dave!

  64. agapeist

    This is marvelous! Thank you!!!!

Trackbacks for this post

  1. 30+ (More) Most Wanted Wordpress Tips, Tricks and Hacks | My Tech way
  2. Redirect Self-Hosted WordPress Site Without Loosing Traffic | HoverDroids
  3. How To Redirect A Web Page WordPress – Web Hosting