Calculate distances From Latitude and Longitude in PHP

Here is a code snippet which allows you to calculate the distance between longitude and latitudes in either miles, kilometres or nautical miles.

<?php

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}
?>

Force www With htaccess

For SEO it is important to use a set standard of how you present your domain. The most popular approach is to use the sub-domain www. Many people don't know this but http://www.domain.com is the same as http://domain.com, you will be sent to the same page but they will have different URLs.
For SEO if a search engine saw http://www.domain.com it will crawl this page, check the content and index the content. Then later if it goes and crawls http://domain.com the search engines will see the same content but on different URLs and think it is duplicate content and therefore will penalise this domain in the search engine ranking.

Consistent URLs

The best way to get around this problem is to make sure that all your URLs on your site use a consistent URL format either www.domain.com or domain.com. It makes no difference which format you choose, the search engine just worries about duplicate content.

Use www

I prefer to make sure all my domains use www sub-domain, this is the format most people are used to seeing on search engines and web sites. If people are used to seeing it then they will not worry about the URL.
Now that you have chosen your preferred format and changed all your links to your site to use this format then you are done, right, wrong! If you stick just with this you run the risk of someone linking to you but forgetting to put a www on the front, the search engines will then crawl through this link and see duplicate content and penalise your domain.
To combat this problem you have a couple of options the best solution is to perform a check of the URL on each page if it doesn't contain the www then perform a 301 redirect to your domain with the www sub-domain. The best way of using this solution is to use htaccess, this will run before any pages are loaded and allows you to change the URL with a 301 redirect.

.htaccess To Redirect URLs

To use htaccess to redirect URL just copy and paste the snippet below and replace example.com with your domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

Add www With HTTP/HTTPS

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Reduce Comment Spam With htaccess in Wordpress

Comment spam is a problem with many blogs, one of the biggest is spam bots crawling sites and automatically submitting comments to your site. The below code snippet will check the HTTP_REFERER on the wp-comments-post.php, it will make sure that you can only get to this page if you are on the current domain.
Below is a snippet you can use on your .htaccess to help reduce the amount of spam coming through on your comments page on your WordPress blog.
Just add the following into your htaccess file to reduce spammers.
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{REQUEST_METHOD} POST 
RewriteCond %{REQUEST_URI} .wp-comments-post.php* 
RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR] 
RewriteCond %{HTTP_USER_AGENT} ^$ 
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L] 
</IfModule>
This snippet will check the the URL coming to the wp-comments-post.php file is from your own domain.