Redirect non-www URLs


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.