I want to stop all the IPs and subdomains completely. Someone edit/modify it so it won’t add IPs and subdomains into database. I only want main (Top-Leven Domain) in my database. Here’s the pattern i am using:
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
if (preg_match($pattern, $url) ) {
header("Location: good.php");
} else {
header("Location: bad.php");
die();
}
A toplevel domain only contains letters. Here is a list of TLDs.
I assume you meant to say ‘second level domains’, but even that will give you trouble. For instance,
.co.ukis a very popular SLD, but it is not used for a website or anything. The domains you mention, are on the third level, below this SLD. (anycompanywebsite.co.uk).So, what I’m saying is that you cannot do this using a simple regex. You will need to have a list of second, third and even fourth levels that are used as if they are a TLD.