Posted By

r4b87 on 09/27/10


Tagged

php redirect 301


Versions (?)

Redirect non-www URLs


 / Published in: PHP
 

This code wil 301 redirect non-www URLs to the www URL

  1. $domain=$_SERVER['HTTP_HOST'];
  2.  
  3. if(substr($domain, 0, 3)!='www'){
  4. $redirect_url="http://www.{$domain}{$_SERVER['REQUEST_URI']}";
  5.  
  6. if($_SERVER['QUERY_STRING']!=''){
  7. $redirect_url.="?{$_SERVER['QUERY_STRING']}";
  8. }
  9.  
  10. header('HTTP/1.1 301 Moved Permanently');
  11. header("Location: {$redirect_url}");
  12. exit();
  13. }

Report this snippet  

You need to login to post a comment.