Peggy Posted March 4, 2010 Share Posted March 4, 2010 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(); ?> Link to comment https://forums.phpfreaks.com/topic/194155-301-redirect-from-www-to-non-www/ Share on other sites More sharing options...
quasiman Posted March 4, 2010 Share Posted March 4, 2010 Tried an .htaccess file? Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC] RewriteRule ^(.*)$ http://yoursite.com/$1 [L,R=301] Link to comment https://forums.phpfreaks.com/topic/194155-301-redirect-from-www-to-non-www/#findComment-1021542 Share on other sites More sharing options...
gwolgamott Posted March 4, 2010 Share Posted March 4, 2010 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. Link to comment https://forums.phpfreaks.com/topic/194155-301-redirect-from-www-to-non-www/#findComment-1021546 Share on other sites More sharing options...
teamatomic Posted March 4, 2010 Share Posted March 4, 2010 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 Link to comment https://forums.phpfreaks.com/topic/194155-301-redirect-from-www-to-non-www/#findComment-1021583 Share on other sites More sharing options...
Peggy Posted March 4, 2010 Author Share Posted March 4, 2010 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 Link to comment https://forums.phpfreaks.com/topic/194155-301-redirect-from-www-to-non-www/#findComment-1021585 Share on other sites More sharing options...
Peggy Posted March 4, 2010 Author Share Posted March 4, 2010 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! Link to comment https://forums.phpfreaks.com/topic/194155-301-redirect-from-www-to-non-www/#findComment-1021590 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.