www and non-www domains - problems and solutions
Thursday, May 3rd, 2007I was developing a website on my testserver, which both myself and my client accessed through client.jeffkee.com subdomain. Throughout development, there were a lot of AJAX modules to be put in - AJAX modules that should not be hacked.
Basically the best way to check for the header is to use the $_SERVER['HTTP_REFERER'] variable, and make an if() statement to ensure that the server-side PHP file pertaining to the AJAX module was called by a file within my own server. The $sitepath variable was set to http://client.jeffkee.com/ for match verification.
Once it was moved to the real server, www.clientserver.com (hypothetically let’s say that was the domain), this caused some problems. Some clients had accessed the site without the www. in front. That means that http://clientserver.com will NOT match the $sitepath variable (which was now set to http://www.clientserver.com/. Options to fix this were the following. And as you can imagine, out of the 3 options I skimmed through in my head over a minute or so, the 3rd one was the charm.
- Make an alternate $sitepath variable. Make $sitepath2 = http://clientserver.com, and make an if statement with the || (means “or” in PHP) so that either one can satisfy the security measurements.The problem with this method is, obviously, it’s time consuming. I had a few AJAX modules, and to change them all would be a hassle, and when adding new AJAX module I would always have to remember to add the alternate variable to the if() statement.
- All the internal links should be re-written with the $sitepath variable put in, so that as soon as you leave to another page it links to http://www.clientserver.com/
Once again. Very time consuming, probably worse than the above option. Also, if one of the AJAX-employing files were to be bookmarked on the client’s side without the www., it would still not work.
- Use an .htaccess file to re-write the URL automatically for any of the files in that location to append to have the www.
This method was the obvious winner. It only took 6 lines of code, not to mention it did not require me to modify any of my other files whatsoever.
Here’s how this works.
The .htaccess file is a method that ONLY works on Apache servers. If you’re on a Windows server, this tutorial won’t help you.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^clientserver.com [NC]
RewriteRule ^(.*)$ http://www.clientserver.com/$1[L,R=301]RewriteCond %{HTTP_HOST} http://clientserver.com [NC]
RewriteRule ^(.*)$ http://www.clientserver.com/$1[L,R=301]
I’m going to be honest with you, I don’t know what all the lines mean, and all I know is that it works. This way, whether the website is typed in as clientserver.com on the URL field or http://clientserver.com, it will automatically re-route via a 301 redirection to http://www.clientserver.com.
Of course, if you want to use this code, you should change the clientserver to whatever server you are working on. Oh, and the .htaccess file should be placed in the folder where you want this to apply, and it will automatically apply to all sub-folders as well!
Maybe this makes me sound like some kind of a nazi or a communist bastard promoting some crap, but it’s really not. The truth is, whenever a friend of mine joined 