Removing all www. and redirect to no www.

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

Posted in Codifically, PHP Script, Plugins, WordPress | Tagged , , , | Leave a comment

MultiSites Part Two (Script)

Yesterday in MultiSites, MultiNetworks and Page Link To’s! I added two plugins…

WP Multi Network
and
Page Links To.

Both of these worked great allowing me to go to Settings>Reading and set a different static page for the home or default page on a 301 or 302 redirect.

Here is how…

Let’s assume the following… (but this is not the way it is…)

Say our domain is:
http://fgbmfi.net/

The page I want to always open as my Default or Home page is:
http://example.com/My-Home-Page/

I want the latest post of my blog to open at:
http://fgbmfi.net/blog/

I also want a tab “Blog” on my menu to open to the latest Blog

On the Page menu I created a new page call it something simple like Home! At the bottom put the URL of the page you want to start with, in our case http://example.com/My-Home-Page/ (static or dynamic is ok) and click if you want to make it a 301 or a 302 redirect. You can also open it in a new window if you like that (I do not!).

Now make another new page and call it Blog. Leave it totally empty – it just needs the title.

Switch to Settings>Reading and on Front Page Displays click on static page. For Front page select the title of your redirect page, for example home. Post page is set to our blank page, blog.

That’s it, no whenever anyone browses to your domain alone – they will end up at your Home pages, perhaps a static page on your domain or another domain that links back to your blog page at:
http://fgbmfi.net/blog/

The only problem I had with this was that I already have a default page working and I did not want to change it’s location!  So here is what I added to my default script. The domains listed here will have their WordPress Blog as the default page. Because FGBMFI.Net is NOT listed it will default to the normal start up site.

I wrote this script and put it in a file called my-start-page.php

$myDomain = $_SERVER['HTTP_HOST'] ;
// You may want to do something here so that
// $myDomain is just what you want.
// For example no www or with a www...
//
//****************************************
// CHECK FOR SPECIAL HANDLING FOR WordPress
// Check $myDomain for special handling
//****************************************
$wparray = explode('=', $_SERVER['REQUEST_URI']);
switch (true){
case ("example.com"==$myDomain):
case ("alantait.com"==$myDomain):
case ("alantait.net"==$myDomain):
case ("vegenesist.com"==$myDomain):
case ('/?p'==$wparray['0']: // For page preview...
require($_SERVER['DOCUMENT_ROOT'] . '/index.php') ; exit;
}
//
// This is where your normal start up is run, perhaps...
require($_SERVER['DOCUMENT_ROOT'] . '/home.php') ;
?>

.htaccess
To make this work you have to edit your .htaccess file in your root directory /…

Something like this…

DirectoryIndex my-start-page.php index.php index.html index.htm index.shtml index.cgi index.php3 index.phtml

What is most important is the my-start-page.php is first like this…

DirectoryIndex my-start-page.php index.php

With this setup The FGBMFI.Net home page is:
http://fgbmfi.net/

The FGBMFI.Net blog is:
http://fgbmfi.net/blog/

Just the way I wanted it! The following ARE listed so they start with the latest Blog post as the default.

http://alantait.com/
http://alantait.net/
http://vegenesist.com/

You can also use the same tools to add a tab on your blog that goes to your site if you start with your blog… For example…

http://vegenesist.com/ has a tab that leads to a small static website. That tab is called VeG and is clearly visible on the Lan the VeGenesist blog.

Codifically Yours,

Lan

Posted in Codifically, Internet, PHP Script, Plugins, WordPress | Tagged , , , , | 1 Comment

MultiSites, MultiNetworks and Page Link To’s!

I decided to migrate a number of my base sites to WordPress.  I think this should serve me well.

So today I loaded MultiSites or a Network of Sites

This allows me to add a number of sites based on domain name. For example Lan.aLanTait.net and Percy.aLanTait.net if I want to.

It gives me in theory an unlimited about of blog sites that end in aLanTait.net including aLanTait.net! Problem is it does not give me a sight called aLanTait.com and I want that!

WP Multi Network

The WP Multi Network Plugin came to my rescue!  Now instead of having only one network ( aLanTait.net ) I can add aLanTait.com and as many other networks that I want (or that I can afford!) Meaning I can just happily add blogs as I need them to start up different long term projects.

Page Links To

The problem now is that each domain defaults to the WordPress Blog!  Maybe I would like to start a different page.  Likewise I would like some external links on the upper tabbed menu… So I plan to add to try Page Links To – to see if this will be a workable solution.

At this point I have four of the seven blogs I am hoping for initially.  Here is a list so far…

aLanTait.com
aLanTait.net
VeGenesist.com
FGBMFI.net

The last site is having some problems so I am working on those.  Each site is different, even though they look pretty much the same at this point.  I am trying to make them workable – later I will work on making them pretty!

Networkly,

Lan!

Posted in Internet, Plugins, WordPress | Tagged , , | 2 Comments

Welcome to the LAN Net Work!

Greetings – this is the First post on the LAN Net Work.

LAN is not a Local Area Network!  I will leave it up to you to decide if Lan is Loving And Nice!  Lan is simply my name, as in aLan!

I do computer work – usually on the Internet!

This is the place where I will post some of the interesting things I am doing.

This morning I am working on my MultiSite / multiNetwork Installation of WordPress. I will tell you more about that later.  For now, I just wanted to get this up and running!

Take Care and Happy Net Working!

Lan

Posted in Internet, LAN, WordPress | Tagged , , | Leave a comment