.htaccess 301 Redirect: Moved Permanently

I’ve already discussed how to create a 301 permanent redirect using PHP that allows you to forward one page to another. But what if you you have an entire site to redirect? What if you’ve changed domain names? There has got to be a better way to redirect your site besides, adding a php 301 redirect to every page. Well there is! It’s called .htaccess.

My next post will be a quick introduction to “.htaccess”.

If you only want to redirect from one page on your site to another, this is the .htaccess code to use:

redirect 301 /oldfolder/oldfile.htm http://www.websitesinaflash.com/new.htm

There are three parts to this redirect (make sure you put 1 space in between each element):

1) “redirect 301”: This declares that you are doing a redirect, and that the code is 301: Permanent Redirect.

2) “/oldfolder/oldfile.htm”: This declares the page you want to redirect from.

3) “http://www.websitesinaflash.com/new.htm”: This is where you want the page to redirect them to.

This is useful when you have renamed pages, and want to make sure google knows where your new pages are, and also any visitors that would be directed to that old page will automatically be directed to the new page.

But what about directing an entire folder or site to another folder or site?
You’ll have to wait for next post. =]


PHP 301 Redirect: Moved Permanently

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 http://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: http://websitedesign.websitesinaflash.com/”);

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 “http://www.websitesinaflash.com” (NOT: “http://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.websitesinaflash.com”)
header(“HTTP/1.1 301 Moved Permanently”);
header(“location: http://www.websitesinaflash.com” . $_SERVER[‘REQUEST_URI’]);

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

-Ashton Sanders