I am reworking the WordPress 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 (substr($_SERVER['SERVER_NAME'],0,3) == 'www'){
// 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 “ful 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…
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