Removing all www. and redirect to no www.

with 4 Comments

I am reworking the WordPress Gamot Global Translator Plugin for my MultiSite install and I found a need to use the domain name as a file directory for my cache files. Then I though about that I will end up with two copies of my cache, one with a www and one without. I realized that I needed to groom the $_SERVER[‘SERVER_NAME’] variable but… How?

Honestly, I do not like to reinvent the wheel so I Googled and I came up with this snip of code. It was repeated in a number of places offered as a solution to redirect www.example.net pages to example.net without the www. Here is the code…

<?php
// if www is subdomain
if ( 'www' == substr( $_SERVER[ 'SERVER_NAME' ], 0, 3 ) ) {
	// remove the www.
	$url = substr( $_SERVER[ 'SERVER_NAME' ], 4, strlen( $_SERVER[ 'SERVER_NAME' ] ) ) ;
	// Add directory and file name to url
	$url .= $_SERVER[ 'PHP_SELF' ] ;
	// redirect the browser to url without www.
	header( 'Location: http://' . $url ) ;
}
?>

At first glance this “Expert” code looks good. However it is a classic example of code that works, part of the time! Let’s take a look at it a piece at a time using a variable substitution… I am going to substitute the variable…

$mySERVER_NAME instead of $_SERVER[‘SERVER_NAME’]

Now I can test it one any php platform without having to do a lot of DNS or server configuration.

header('Location: http://'.$url) ;

This looks fantastic, we are going to feed it something called $url and it will redirect.

<?php
$url = '' ;
if ( substr( $mySERVER_NAME, 0, 3 ) == 'www' ) {
	$url = substr( $mySERVER_NAME, 4, strlen( $mySERVER_NAME ) ) ;
	$url .= $_SERVER[ 'PHP_SELF' ] ;
}
echo "url = $url<br /><br />" ;
?>

If we feed this code a

$mySERVER_NAME = 'www.example.net' ;

The output will be example.net and that looks pretty good…

But in the real world we have domains like…

$mySERVER_NAME = 'wwwservers.net' ;

The out put should be nothing – meaning no change, but instead we get an output of ervers.net because the conditional test was badly formed. Take a look:

if ( substr( $mySERVER_NAME, 0, 3 ) == 'www' ) {
if ( substr( $mySERVER_NAME, 0, 4 ) == 'www.' ) {

In the second example we are looking at the first FOUR characters including the . (dot). This way www.example.net is matched and removed but wwwservers.com remains unmodified.

A better way to do the replacement may be with preg_replace and a simple Regular Expression. Again searching the Internet I found only two websites publishing a solution, both had the same preg_replace and amazingly both would fail sometimes!

$url = preg_replace( '/www\./', '', $mySERVER_NAME ) ;

If you fed this our www.example.net you would indeed get back example.net. But what if you fed it another realworld domain name… www.mywww.net ~ My oh my, what you would get back is only “mynet“!!! This expert would let you find and replace with nothing both copies of www. – including the one you want!

Ok, let’s take a look at the corrected code…

$url = preg_replace( '/www\./', '', $mySERVER_NAME ) ;
$url = preg_replace( '/^www\./', '', $mySERVER_NAME ) ;

That little ^ tells the code to replace only the www. at the beginning of the string… www.mywww.com to mywww.com

Now let’s take a look at the last line of this programming ace’s code.

// Add directory and file name to url
// - - This is the full new URL
$url .= $_SERVER[ 'PHP_SELF' ] ;

Accord to HIS notes this is going to build the “full new URL” to redirect the page to! Well $_SERVER['PHP_SELF'] would not add anything passed the file name. For example…

www.example.net/?p=19&preview=true
may likely end up as
example.net/index.php

The better code would be…

$url .= $_SERVER[ 'PHP_SELF' ] ;
$url .= $_SERVER[ 'REQUEST_URI' ] ;

The result would be…
www.example.net/?p=19&preview=true
changed to
example.net/?p=19&preview=true

Now all we need to do is switch back our variable and put it all together like this…

Note: This would be the code to copy and place at the top of your default file, perhaps named index.php

if ( substr( $_SERVER[ 'SERVER_NAME' ], 0, 4 ) == 'www.' ) {
	$url = preg_replace( '/^www\./', '', $_SERVER[ 'SERVER_NAME' ] ) ;
	$url .= $_SERVER[ 'REQUEST_URI' ] ;
	// Permanent redirection
	header( "HTTP/1.1 301 Moved Permanently" ) ;
	header( 'Location: http://' . $url ) ;
	exit() ;
}
?>

You can use that code at the top of your php page to redirect all people and search engines to your site without the www. That may improve your page rank because you will not have duplicate content with your www page! Notice that I added another header? This makes it a permanent 301 redirection, which reassigns page rank. That is instead of the temporary 302 redirect which does not assign page rank in search engines.

And the code I need to create a directory from the current domain name without any “www.” ~ Just one line…

$url = preg_replace( '/^www\./', '', $_SERVER[ 'SERVER_NAME' ] ) ;

I hope you enjoyed this little code testing with me!

Codifically Yours,

Lan

4 Responses

  1. Dan Hatcher
    | Reply

    Ok, You have this at the top of the page:

    Example #1:

    And you have this at the bottom of page:

    Example #2:

    if ( substr( $_SERVER[ ‘SERVER_NAME’ ], 0, 4 ) == ‘www.’ ) {
    $url = preg_replace( ‘/^www\./’, ”, $_SERVER[ ‘SERVER_NAME’ ] ) ;
    $url .= $_SERVER[ ‘REQUEST_URI’ ] ;
    // Permanent redirection
    header( “HTTP/1.1 301 Moved Permanently” ) ;
    header( ‘Location: http://‘ . $url ) ;
    exit() ;
    }
    ?>

    I take it, after the explanation, that #2 is the better choice because of the 301 redirect. But (I may have just missed it) “Where do you insert this code?” In the htaccess file? One of the WP files? ? 🙂

    I have used redirects in my hosting account CPanel where I input in the form, “Incoming link: http://www.mydomain.com = http://mydomain.com
    I have heard that this is more reliable than messing with the htaccess file (which you can mess up very easily and sometimes has a mind of it’s own) Is this a better and easier solution or is this a totally ineffective way to go about doing the same thing you mention in this post?

    Dan

    • Lan
      | Reply

      Hi Dan, sorry the delay…

      This redirect works best for building a dynamic website. For example, wordpress is dynamic, every webpage starts with index.php. This would then be placed at the top of that page.

      This is NOT for the .htaccess file

      There is a way of doing it in .htaccess – but that would be a strict rule for everything on your sites. I have about 130 sites on my server and would not want to restrict them in that way. This allows me to redirect just a subset – whichever ones touch this dynamic page.

      Actually, I have never used CPanel or anything like that, it adds another layer that I never felt I needed.

      The BEST way to check to see if something is working is to check your HTTP Response Header

      There are many tools out there but one simple one my wife likes is the Web Sniffer!

      http://web-sniffer.net/

      I put in:
      http://www.naturespharmacy.org/
      And it returned:
      Status: HTTP/1.1 301 Moved Permanently
      Location: http://naturespharmacy.org/

      That is good! 301 is very search engine friendly.

      Then I put in:

      http://naturespharmacy.org/

      And Web-Sniffer returned…

      Status: HTTP/1.1 200 OK

      Just what I wanted.

      The old way was to point everyone to the www page. That is also SEO friendly. But it is becoming less popular.

      austinseoacademy.com

      Yields a:

      Status: HTTP/1.1 301 Moved Permanently

      With the WWW yields a:

      Status: HTTP/1.1 200 OK

      So basically you are telling search engines (and everyone else) that they MUST use the www.

      For SEO in this regard, your site is set up ok. When it is listed it will be with the www.

      Codifically,

      Lan

  2. Sean Wilson
    | Reply

    Hi Lan,

    Apologies if this is off-topic, but you say “I am reworking the WordPress Gamot Global Translator Plugin for my MultiSite install ”

    Did you get it working? I’ve been struggling with this for a couple of weeks now!

    I saw your post on the WP forum at http://wordpress.org/support/topic/plugin-global-translator-multisite-problems.

    I’m using WordPress 3.1.3 with Multisite enabled.

    Any help would be much appreciated!

    Many thanks,

    Sean

  3. Lan
    | Reply

    Hi Sean, I did have it working but not ready for prime time. I was working one 3.0.x not really sure. Because of a number of close friends and family dying this year and some personal health challenges in my own life I pretty much abandon that project. Sorry.

Leave a Reply