Simple page or section redirect in WordPress

Recently we had a chance to re-code the old client site: Graniti-Sušec. The site was first developed back in 2003. and was in fact the first commercial (non-personal) site done with web standards in Croatia.

At that time I was much more involved with ASP 2.0 and merely heard about PHP. But since then — my focus shifted far, far away from ASP, and we decided not to fix the ancient codebase, but to move the backend to WordPress.

With the migration, we had to take care about the new URL scheme. The language part of the previous scheme was a full-length language name and therefore clumsy. The transition had to be seamless, and we had to make sure not to cut-off visitors landing to the website from Google.

The solution was quite simple. Each page was organized as a child page of one of two parent pages: /hr/ and /en/. We created two additional first-level pages with slugs corresponding to the old language URL parts — /hrvatska/ and /english/ — and set a template for both pages to “Page Redirect”. This is how it’s done step by step:

Step 1: Create a page template

In your WordPress theme folder create a new blank file: page-redirect.php, paste in the code below and save it. Make sure that there’s no space left before <?php or after ?> or it might throw an error.

Template Name: Page Redirect 
    header('HTTP/1.1 301 Moved Permanently'); 

    $redirect_url = get_post_meta($post->ID, 'redirect_url', true); 

    if (!empty($redirect_url)) { 
        header('Location: ' . $redirect_url); 
    } else { 
        header('Location: /'); 

Step 2: Add a new page

Create a new page in WordPress (Pages » Add New), and paste the old URL into Permalink field — right below the title field.

Step 3: Add a custom field

In the Custom Fields box, add a new custom field, named redirect_url and set the value to match the new URL.

Step 4: Choose a page template

In the attribute box in WordPress (usually on the right hand side), there is the Templates drop-down. Select the “Page Redirect” option and hit Publish.

Type the old URL in the browser address bar, and it should be instantly redirected to the new URL. WordPress will also automagically redirect all child pages, so in our example /english/marble/ redirects to /en/marble/.

I hope it helps, let me know if you have a different approach to a similar problem.

Marko Dugonjić is a designer specialized in user experience design, web typography and web standards. He runs a nanoscale user interface studio Creative Nights and organizes FFWD.PRO, a micro-conference and workshops for web professionals.

Interested in more content like this?