Posted By

traeregan on 10/07/09


Tagged

www force


Versions (?)

Force WWW Subdomain


 / Published in: PHP
 

URL: http://www.bradino.com/php/force-www-subdomain/

Sometimes you do not want users to access your domain without the www. If you want to force www.yourdomain.com and redirect yourdomain.com to www.yourdomain.com here is a simple way to do it.

  1. if ((strpos($_SERVER['HTTP_HOST'],'www.')===false)) { header('Location: http://www.yourdomain.com'); exit(); }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: MMDeveloper on October 8, 2009

and if you want to apply it globally across the entire website without modifying every php file (and you're on a linux server), you can use .htaccess

RewriteEngine On RewriteCond %{HTTPHOST} !^www.YOURSITE.com [NC] RewriteCond %{REQUESTFILENAME} !-f RewriteRule ^(.*)$ http://www.YOURSITE.com/$1 [R=301,L]

That will redirect everything (html, php, jpg, gif, pdf, etc.. EVERYTHING) and will also invoke a 301 redirect header to tell search engines to gracefully update their links when visited so you don't lose your SEO rank.

Posted By: MMDeveloper on October 8, 2009

ok not sure why it didn't preserve the "new lines"...

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mechanicmatt\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://www.mechanicmatt.com/$1 [R=301,L]

You need to login to post a comment.