Taking tutorial submissions! Please email them to dave[AT]icemelon[DOT]com for review. Thanks!

Latest Tutorials
IceMelon IM: Add IM Functionality to phpBB [Misc] Want to add site-wide member IM to your phpBB community? Sure you do. Simple step-by-step instructions inside.

IceMelon IM: Add IM Functionality to vBulletin [Misc] Want to add site-wide member IM to your vBulletin community? Simple step-by-step instructions inside.

Crawl Your Site for PageRanks [PHP] So you know your site has a PageRank of 8. What about the PRs of all the inner pages? This tutorial will teach how you to automate the process of grabbing all these PRs.

Create CAPTCHA images [PHP] Worried about bots? Then create CAPTCHA images to prevent bots from taking advantage of your site.

Convert SHN and FLAC files to MP3 [Misc] Low on disk space and have an insensitive ear? Here are some steps to converting SHN and FLAC files to MP3 format.

[View All]

Become a sponsor for $15/month. Link is sitewide - PR5 homepage, 20+ PR4 pages, 90+ PR3 pages. Email dave[AT]icemelon[D0T]c0m.

Awesome Tutorials

Force the 'www' into the URL
By dave

In most cases, you will want to ensure that all your web pages have the "www" within their URLs. (Either all have the "www" or all don't.) Here are two reasons why this is important:

1) It will improve your Google pagerank. You may have noticed that in most cases, domain.com and www.domain.com have different page ranks. By forcing all domain.com/etc pages to redirect to www.domain.com/etc pages, you will not be compromising your pagerank.

2) If your site deals with cookies, you want to only store one set of cookies in your visitor's computer. This way there will never be the situation where a visitor is logged out of domain.com, but still logged into www.domain.com

Ok, now let's get to the code:
if(preg_match("/^icemelon.com/i", $_SERVER['HTTP_HOST'])) {
    $changeUrl = str_replace("icemelon.com", "www.icemelon.com", $_SERVER['HTTP_HOST']);
    $appendUrl = '';
    if(!empty($_GET)) {
        $appendUrl = '?';
        foreach($_GET AS $var=>$val)
             $appendUrl .= "$var=$val&";
    }
    echo "<script> window.location = "http://$changeUrl" .$_SERVER['PHP_SELF'] .$appendUrl .""; </script> ";
}

Notice how the whole thing is wrapped within an IF statement. This statement's condition checks whether the URL begins with "icemelon.com" (using preg_match). If this is true, then we want to redirect to this page's "www.icemelon.com" analog.

Next, the str_replace function adds in the "www" by replacing "icemelon.com" with "www.icemelon.com." The next IF statement checks for values passed in the URL; e.g.
example.php?this=stuff&andThis=stuff&youKnow=whatIMean
With a FOREACH loop, this trailing section of the URL is regenerated and stored into $appendUrl. Finally, using simple Javascript, we redirect the visitor to the appropriate www.domain.com page.

Now, you just need to include that code into the header file of your website.

EDIT!
As pointed out by a kind reader, Javascript can be disabled by your website's viewers. Therefore, he recommended the use of header to redirect instead:
if(preg_match("/^securityfocus.de/i", $_SERVER['HTTP_HOST'])) {
   $changeUrl = str_replace("securityfocus.de", "www.securityfocus.de", $_SERVER['HTTP_HOST']);
   $appendUrl = '';
   if(!empty($_GET)) {
       $appendUrl = '?';
       foreach($_GET AS $var=>$val)
            $appendUrl .= "$var=$val&";
   }

header("location:http://$changeUrl".$_SERVER['PHP_SELF'].$appendUrl."");
}

EDIT II!
According to Matthew Callis, the proper way to do this is with the .htaccess file. The script for that can be found here: [http://no-www.org]. Below is the code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

P.S. Check out my new site TheManWhoSoldtheWeb.com, where I publish guides and scripts on Internet Marketing and SEO. Here is a limited time freebie: the Rapid Google Indexer.


» Bookmark this Tutorial
» Bookmark IceMelon
Icemelon -- Force the 'www' into the URL -- PHP, CSS, Javascript Tutorials, & More!
  © 2005-2010 Icemelon.com   Email: dave[AT]icemelon[D0T]c0m