I am having a hard time with my host to set up a 301 redirect. They do not support .htaccess, so I am talking with the customer service to see how we can solve the problem. one solution they proposed me is to put the code below on the header. <? header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.newdomain.com/newpage/newurl.htm"); exit(); ?> now, does anyone familiar with php or redirects know if that will create a 301 even for the purposes of google optimization and PR? thanks
ok, but how do I make the www.domain redirect to the one without www? because they use the same index.php file don't them? therefore I am not sure where to place the mentioned code...
You just need to place the code at the top of your index.php file: <? header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.newdomain.com"); exit(); ?> Code (markup): But it is better to do that through .htaccess as it will do for all of your directoires under the root public folder. And yes they use the same index.php file Regards
If you must use PHP to add or remove the www, I changed the code a little bit below. It would be best if you put it in a header file that's included on all pages. In both examples, I used the SERVER_NAME variable so you don't even need to modify the code for your domain. Both will check for www before redirecting, to avoid a loop or execute unnecessarily. I also added the REQUEST_URI on the end of the redirect, so it will work on any page. That could come in handy if you already have mixed inbound links to internal pages. This one will redirect non-www to www, if it's not already there. <?php if(substr($_SERVER['SERVER_NAME'], 0, 4) != 'www.') { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"); exit; } ?> Code (markup): And this will remove www: <?php if(substr($_SERVER['SERVER_NAME'], 0, 4) == 'www.') { $domain = str_replace('www.', '', $_SERVER['SERVER_NAME']); header("HTTP/1.1 301 Moved Permanently"); header("Location: http://$domain{$_SERVER['REQUEST_URI']}"); exit; } ?> Code (markup): Hope that helps. Personally, though... I can't picture using a host without .htaccess.
thanks man, very helpful i also did find a wordpress plugin that implements the php code automatically, pm me if you need it