PHP, SEO, Webmaster

PHP 301 Redirect: Moved Permanently

1 Comment

Through years of programming websites and Internet database software, etc. I’ve decided to work mostly with PHP. PHP is a server-side scripting language that helps to create interactive websites. It is very useful for creating dynamic pages. (Pages that have one format but fill in different information depending on what information it is given.) This is common in any online store. Every product has the exact same layout, but it has different pictures, and words.

A great thing about PHP is it’s free. It’s largest server-side scripting language competitor, Microsoft’s ASP, isn’t. The PHP syntax is widely used and is very similar to C and Perl.

If I ever have a question about PHP or need information, I always go to https://W3Schools.com. It’s also a great place for beginner programmers to get their foot in the door.

Here is how to us PHP code to make 301 Redirects:

301 Redirect: Moved Permanently
This code is for page redirecting. If you ever change a page name on your website, or take it down, you need to redirect that link to another page. This is because if you’ve had that page up for any period of time, the search engines like google.com or yahoo.com still think that it’s there. Or another site may be linking to it. This will save visitors from getting lost and never seeing your site when they cliked on your link.

Here is the PHP code:

<?
header(“HTTP/1.1 301 Moved Permanently”);
header(“location: https://www.websitesinaflash.com/”);
exit();
?>

301 Redirect to “www.”
This code I programmed because I wanted to make sure that whenever someone comes to my site, they come to the “https://www.www.websitesinaflash.com” (NOT: “https://www.websitesinaflash.com“) So now whenever someone types in my website name without the “www.” It will automatically redirect.

“Who Cares if there is a ‘www.’ in front of your website or not?
The biggest reason for this is so that search engines don’t index two version of the same page. For example, if a Search Engine’s bot find a link to my sight that doesn’t have the “www.” It will index the entire site without it. And if that happens, I’ll have two exact duplicates of my website.

“Wouldn’t that be a good thing?”
No. Search Engines will ding you if you have duplicate content. They see it as an attempt to cheat the system. I knew a man who changed the hosting and domain name for his website. But he didn’t take down his old site. After a couple months, his old site which had had 3-4 Google Page Rank, now had 0 (zero). He realized this and took down his old site, and after half a year, he was back to normal.

Here is the PHP code:

<?
if($_SERVER[‘HTTP_HOST’] != “www.www.websitesinaflash.com”)
{
header(“HTTP/1.1 301 Moved Permanently”);
header(“location: https://www.websitesinaflash.com” . $_SERVER[‘REQUEST_URI’]);
exit();
}
?>

And there’s my two cents on 301 Redirecting with PHP.

-Enjoy
-Ashton Sanders

1 Comment

  • Using .htaccess to redirect:This is the rough equivalent to your php script using a couple of lines in your .htaccess file. These lines use the Apache webserver’s mod_rewrite engine.# Add .www if missingRewriteCond %{HTTP_HOST} !^www\. [NC]RewriteRule ^(.*)$ http ://www.%{HTTP_HOST}/$1 [R=301,L]This file should be placed in your web site’s root directory or these lines can be added to one that already pre-exists.Note: This will not work correctly with subdomains without a bit more time and effort.Using Apache’s mod_rewrite has the added benefit of redirecting before php attempts to parse any page. Also, it saves having to add or include this extra script to every page on your site. Finally, the last benefit would be that it works for any type of document you are serving (ie: *.html) not just php documents.

Leave a Reply

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