Jump to content

301 redirect from www to non www.


Peggy

Recommended Posts

I've been reading about SEO and have found out that it is important to decide weather or not I want to use http://www.example.com  http://example.com as the url for my site. So now I want to put a 301 redirect in from http://www.example.com to http://example.com . I found this code but it I don't think it is what I am looking for. Becouse I only want someone to exit if they come in as http://www.example.com. Can you please help?

<?php

header("HTTP/1.1 301 Moved Permanently");

header("Location: http://www.newdomain.com/newpage.html");

exit();

?>  :shrug:

Link to comment
https://forums.phpfreaks.com/topic/194155-301-redirect-from-www-to-non-www/
Share on other sites

exit shouldn't matter. That just kills the php script. The header should have sent you to your site before that killed itself anyways. It's a tie up loose string thing is the only reason it's there. To make sure any PHP code after the redirect does not parse after it's done redirecting you.

You dont want to even use the php code you showed. That would be if you had a page like:

domain.com/animals.php and wanted instead to use domain.com/creatures.php. That would would go in the old animals.php page to redirect to the new creatures.php page.

 

Use the htaccess example your have already been given.

 

Name the file .htaccess(note the dot at the front of the file name). That means its a hidden file. Before you make a new htaccess file check to see if there is an existing one, if there is just add the code to the bottom of it. If there is not already one then go ahead and create one. It would go in the root of your website.

 

 

HTH

Teamatomic

What you said about the exit(); makes sense to me. I just thought about a new problem. I was going to put this code in the header. I have over 3,000 pages running off this header. Obviously I can't redirect them to the home page that will not work. I can make it so that it only shows up on the home page, but I don't think that that will solve the "duplicate content issues" because someone would still be able to enter the page using http://www.example.com/page1   instead of http://example.com/page1

I will use the .htaccess file thank you this makes sense.  I watched a video yesterday put out by Google.  They were talking about using a 301 redirect I assume that this code is technically also called a  301 redirect. (Just trying to make sure that I do what ever makes the search engines happy!!!)

Thanks Again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.