List of unique website addresses

    Suppose you need to make a database table with a list of addresses. Moreover, each site should be mentioned only once. A very common task is to take, at least, site directories.

    A site can be identified by a domain name. If we lived in an ideal world, then at this place my post would have ended. :) But, everything is just beginning ...

    Most of the sites are accessible (HTML is given or a redirect occurs) at two addresses: example.com and www.example.com . In an almost perfect world :) we would just drop “www.” and scored on this problem. But there are some lopsided individuals who provide their site with only one address, “only from www.”.

    The solution is:
    CREATE TABLE test (www CHAR (4), hostname CHAR (64) NOT NULL, UNIQUE (hostname));

    We separate the particle “www.” and store it separately, all the same, it carries no semantic load. But now, using a normal domain name, you can make a unique index.

    To make a selection on such a table, you can create a view (VIEW), or you can simply add the CONCAT operator to SELECT:
    SELECT CONCAT (www, hostname) AS url FROM test;

    Z.Y. “Www.” - This is archaism , it is inconvenient and impractical. Let's do without discussions on this topic. ;)
    Z.Y.Y. And I once wrote about how to make a redirect for my site with “www.” to the regular version: Tips & Tricks .

    Also popular now: