PHP ROUTE

301 redirect

How to Use 301 Redirect in Your WordPress Blog

To skip the technical jargon, a 301 redirect is a web code that redirects one webpage or website to another.

Web designers use them all the time. If a you need to change the name of your website domain, a subfolder or even a single webpage, a URL redirect is how you change the name without losing all your hard earned traffic.

There are multiple types of URL redirects. The way these are broken down is Redirect Type – Brief Translation – Benefits – Search Engine Directions.

  • 301 – Moved Permanently – Passes link equity and saves PageRank – Use new URL from now on.
  • 302 – Found – Page temporarily located at a different URL – Continue to use old URL.
  • 303 – See Other – Page found under a different URL – New URL not a substitute for originally requested resource.
  • 307 – Temporary Redirect – Page temporarily located at a different URL – Redirection MAY be altered on occasion – Continue to use original URL.

For the sake of having a good example, let’s say that you own www.website.com and had an informational page at www.website.com/WhyWeRock.php. If you wanted to change the page URL to something more like www.website.com/OurCustomersRock.php, you would lose all the traffic that you currently get to the old page at www.website.com/WhyWeRock.php. However, if you implement a 301 redirect from the old page to the new one, anybody who goes to the old page or URL will be redirected to your new page and URL.

You could also redirect your whole domain from the boring and innocuous www.website.com to the more exciting www.daredevil.com. A 301 redirect will get all your visitors from the old site that they have bookmarked, to your new site that they haven’t bookmarked yet.

PHP Redirect

If you have a website created in PHP and you need to redirect a page, you need a PHP 301 Redirect. And you have 2 different options depending on what exactly you need. You can redirect a single page to another page, or you can redirect an entire website from website.com to www.website.com.

How to redirect a single page

DO NOT delete the original file. Go to the old PHP file that needs to be redirected and remove all the code and replace it with this. And just to state the obvious, “http://www.new-url.com” needs to be replaced with the URL of the page you want it to redirect to. ;-)

<?
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.new-url.com” );
?>

How to redirect to www.website.com

It should be noted that for SEO purposes, a link that goes to website.com and a link that go to www.website.com are NOT the same thing unless you have a 301 redirect going from website.com to www.website.com. If your web visitors see the www version, but your links go to the non-www version, it is entirely possible that they will not count or pass link equity for your SEO efforts.

If you want to have www. at the beginning of your domain, put this code on every single page:

<?php
if ($_SERVER[‘HTTP_HOST’] != ‘www.domain.com’){ header(“Location: http://www.domain.com”.$_SERVER[‘REQUEST_URI’]);
}
?>

How to redirect to website.com

If you don’t want to have www. in your domain, put the following code on every single page:

<?php

if ($_SERVER[‘HTTP_HOST’] == ‘www.domain.com’){
header(“Location: http://domain.com”.$_SERVER[‘REQUEST_URI’]);
}
?>

If this post has helped you, let us know by posting a comment at the bottom. Spam comments will be deleted.

WordPress Page/Post 301 Redirect

This is a really easy one! If you need to redirect some pages in WordPress, you need the plugin “Redirection”.

What makes John’s redirection plugin so freaking cool isn’t just the ability to easily redirect pages in WordPress, but that it has logs of pages that come up with 404 errors so that you know which pages need to be redirected to a new page! That’s right, it logs failed pages for you so that you can send that traffic to another, more valuable, location. Sweet!

Here’s how it works

Let’s say, for instance, that you have a URL of http://www.website.com/useful-info/ but you want to change the name to http://www.website.com/ultimate-info/, without losing traffic or SEO link equity, no problem.

  • Once Redirection is installed and activated, go to the “Installed Plugins” page and find Redirection.
  • Click on the link “Settings” immediately below it. This will take you to a screen that has your options on redirecting one page to another.
  • Simply type the old URL (http://www.website.com/useful-Info/) into the box next to “Source URL”
  •  Now enter the new page URL (http://www.website.com/Ultimate-Info/) into the box next to “Target URL”.
  • Click on the “Add Redirection” button.
  • Congrats! You’ve successfully done a WordPress 301 Redirect!

htaccess 301 Redirect

Redirect Only Subpages

If you need to 301 redirect all the subpages of a domain but don’t want to redirect www.domain.com itself, you can use the following bit of code.

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

 

Redirect to WWW or to non-WWW

Some people want to have the WWW at the beginning of your URL. Other people think it looks grittier not to have it. Whichever you prefer, and thankfully it doesn’t truly matter, is up to you. Doing a redirect from websitecom to wwwwebsitecom is fine as long as it is a correct 301 redirect. It’s also perfectly acceptable to redirect wwwwebsitecom to websitecom.

It’s just best if you do choose 1 way or the other for SEO because Google can view website.com and www.website.comas separate sites. This means that if you have incoming links going to both website.com and www.website.com there’s a chance that they will both build up different pages in Google’s eyes. So to correct that, here’s how you can make everything look uniform using the htaccesss file.

Redirect to WWW with the htaccesss file

To edit the .htaccess file, follow the instructions above and insert the following code at the head of the file.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) http://www.website.com/$1 [R=301,L]

Redirect to non-WWW with the htaccesss file 

RewriteEngine On
RewriteCond %{HTTP_HOST} !^website.com$
RewriteRule (.*) http://website.com/$1 [R=301,L]

Redirect 404 Errors to home page

If you have multiple pages that come up with a 404 error and you don’t want to take the time to redirect each and every one, I highly recommend entering the following code into your .htaccess file. It’ll automatically redirect all 404 error pages to your home page, thus saving your SEO and keeping people on your site.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

You just created a 301 redirect.

What Needs To be Redirected?

Remember at the beginning of the post when I mentioned that Redirection keeps logs of pages that create 404 errors? Well, here’s how you find them.

  • In Redirection, “Redirects” is currently bold. This is a tab. Click on “Log”
  • As you see, there are source URLs there. Copy one and go back to the “Redirects” tab and follow the instructions above.
  • Don’t forget, you will need to enter your domain in the for of “http://www.your-domain.com” before the source URL that the log gives you.

I know, it would be great if John wrote in that you could click the button and add the source URLs to a list to be redirected. Well, maybe in the next version.

Now you know which pages need to be redirected to new pages! :-)

Have questions or confused about something WordPress Related? Join Our Discord Server & ask a Question

Leave a Comment

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

Scroll to Top