Skip to content

Instantly share code, notes, and snippets.

@infn8
Last active December 29, 2015 16:19
Show Gist options
  • Save infn8/7696301 to your computer and use it in GitHub Desktop.
Save infn8/7696301 to your computer and use it in GitHub Desktop.
301 Redirect template for wordpress
<?php
/*
Template Name: Redirect
*/
if(! empty($url = get_post_meta(get_the_id(), 'redirect', true))){
header("HTTP/1.1 301 Moved Permanently");
header("Location: ". $url);
die();
}
?>
<html>
<head>
<title>Danger Will Robinson, Danger</title>
</head>
<body>
<h1>Something is wrong. </h1>
<p>Please set a Custom field called 'redirect' with the value of the URL you want to redirect to. <a href="http://codex.wordpress.org/Custom_Fields">More info here.</a> </p>
</body>
</html>
@infn8
Copy link
Author

infn8 commented Nov 28, 2013

To Use This Properly

  1. Save this redirect.php in your theme's root folder.
  2. Create a new page that will resolve to the URL you want redirected. ex: your-site.com/cool-page/
  3. add a custom field called 'redirect' in the page
  4. Select the 'Redirect' Template in the Template dropdown on the right side.
  5. Publish the page

If you want to redirect the homepage then set the homepage to use that page in the Options > Reading setting in the admin panel http://your-site.com/wp-admin/options-reading.php

More info here http://codex.wordpress.org/Creating_a_Static_Front_Page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment