In this lesson we will teach you two ways to redirect links in WordPress Without any plugin.
In the first method, we hardcode the source and destination links into our code, This method is easier to set up and is suitable for small number of links.
In the second method, which requires more setups, we perform dynamic redirection using custom fields. If you have many links for redirection, this method will be ideal for you.

Step 1: Get the From(Old) and To(New) URL Slug

What is slug? The “slug” is that part of the URL without the domain name.
Imagine the URL is “https://yoursite.com/product-category/blue-shirt/” , the slug would be “/product-category/blue-shirt/”.

url slug in wordpress

In this example we will redirect “https://redpishi.com.com/old-product-category/old-blue-shirt/” to “https://redpishi.com.com/new-blue-shirt/“. The slugs will be as follows:

from: /old-product-category/old-blue-shirt/
to: /new-blue-shirt/

First method: (Hardcoded URL)

Step 2: Add custom code in functions.php

In the code below change the [from slug] and [to slug] to your “from” and “to” slugs, then copy and save this code in your child’s theme functions.php.
If you do not know how to make a child theme, read the following article.
Create child theme in WordPress step by step [without plugin]

function redirect_page() {

     if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
        }
        else {
        $protocol = 'http://';
    }

    $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $currenturl_relative = wp_make_link_relative($currenturl);

    switch ($currenturl_relative) {
    
		case '[from slug]':
			$urlto = home_url('[to slug]' );
			break;

        default:
            return;
    
    }
    
    if ($currenturl != $urlto)
        exit( wp_redirect( $urlto ) );


}
add_action( 'template_redirect', 'redirect_page' );

That’s it.
If you have more links to redirect, copy the following portion of above code and paste the rest of the links in it.

case '[from slug2]':
	$urlto = home_url('[to slug2]' );
	break;

Second method: (Build a simple dashboard with the help of custom fields)

Step 2: Add custom code in functions.php

Delete the previous code and copy and save this code in your child’s theme functions.php.


/* redirect by redpishi.com */ 
function redpishi_com_redirect() {
	$redirectArr = array();
	$id = get_page_by_title( 'redirect' )->ID;
	$rowArr = get_post_meta($id);	
	foreach ($rowArr as $key => $value) {	
	 if (strpos($key, "/") === 0) {
    $redirectArr[trim($key)] = trim($value[0]);	}			
	}
     if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
        }
        else {
        $protocol = 'http://';
    }
    $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $currenturl_relative = wp_make_link_relative($currenturl);
	if (substr($currenturl_relative, -1) == '/') { $currenturl_relative = substr_replace($currenturl_relative ,"",-1);}
	$forceredirect = 0;
	foreach($redirectArr as $from => $to) {
		if (substr($from, -1) == '/') { $from = substr_replace($from ,"",-1);}
		if( $currenturl_relative == $from) {
			$forceredirect = 1;
			$urlto = home_url($to);
		}}

    if ($forceredirect == 1) {
		 wp_redirect( $urlto);
 		   exit;		
	}
}
add_action( 'template_redirect', 'redpishi_com_redirect' );

Step 3: Build a page and activate custom fields

Create a new page in WordPress and name it redirect. (Important: page name must be redirect).
Check if Custom fields is active on the screen, if not enable it. Go to the page settings (3 dots at the top right of the screen) and select Preferences from the menu, Then select the Panels tab and make sure the Custom fields option is on.

activate Custom fields in WordPress

Next, change Visibility to Private (for security Reasons) and publish the page.

Step 4: Enter and save your links in Custom fields

In the last step, create and save a Custom field for each link. Note that we still use slugs (step 1). In the name field put “from slug” and in the Value field put “to slug” and click on Update.

Redirect Pages In WordPress by Custom fields

You can add as many Custom fields as you like.
Congratulations, you successfully created a redirect link management panel.

If this article is difficult for you to read in text, you can watch the video version below.


Have you started redirecting pages on your site? Which method do you prefer? Let us know in the comments below.

Are there any SEO implications when using the redirection method described in the article?

The redirection method described in the article uses the built-in WordPress functionality and is considered SEO-friendly. When implemented correctly, the 301 redirect will inform search engines that the original page has permanently moved to a new location, transferring the SEO value and link equity to the new page. This ensures that any existing backlinks to the old page will contribute to the new page’s search engine ranking. However, it’s essential to update internal links to the new page to provide a better user experience and reduce unnecessary redirects.

Can I redirect multiple pages at once using the method described in the article?

The method described in the article focuses on redirecting one page at a time. To redirect multiple pages at once, you can use a WordPress plugin like Redirection, which allows you to create and manage redirects in bulk more easily. Alternatively, you can manually add multiple redirect rules to your .htaccess file or use custom code snippets in your theme’s functions.php file, but these methods require more technical knowledge and should be implemented with caution.

How can I test if the redirect is working correctly?

Answer: To test if the redirect is working correctly, you can follow these steps:
– Clear your browser cache to ensure you’re not viewing a cached version of the page.
– Open a new private or incognito browser window.
– Enter the URL of the original page that you’ve set up the redirect for.
– If the redirect is working correctly, you should be automatically redirected to the new page.
– To confirm that the redirect is a 301 (permanent) redirect, you can use an online tool like Redirect Checker or an SEO browser extension like the Ayima Redirect Path.
By testing the redirect, you can verify that your visitors and search engines will be sent to the correct new page.

Share this post
Maya
Maya

Hi, my name is Maya and I’m a WordPress plugin developer. I created this website to share some of the helpful codes that I’ve used in my own projects.
If you’re looking for a custom plugin for your website, you can contact me by clicking on Hire a developer in the menu. I’d love to hear from you.

Articles: 56

Leave a Reply

Your email address will not be published. Required fields are marked *