0

Is there any way to accept a URL and change it's domain to .com ?

For example if a user were to submit www.example.in, I want to check if the URL is valid, and change that to www.example.com. I have built a regex checker that can check if the URL is valid, but I'm not entirely sure how to check if the given extension is valid, and then to change it to .com

EDIT : To be clear I am not actually going to these URL's. I am getting them submitted as user input in a form, and am simply storing them. These are functions I want to do to the URL before storing, that is all.

Edit 2 : An example to make this clearer -

$url = 'www.example.co.uk'
$newurl = function($url);
echo $newurl

which would yield the output

 www.example.com
Sainath Krishnan
  • 2,089
  • 7
  • 28
  • 43

4 Answers4

1

Are you looking for something like this on the server side to replace a list of selected TLDs to be translated to .coms?

<?php
    $url = "www.example.in";
    $replacement_tld = "com";

    # array of all TLDs you wish to support
    $valid_tlds = array("in","co.uk");
    # possible TLD source lists
    #     http://data.iana.org/TLD/tlds-alpha-by-domain.txt
    #     https://wiki.mozilla.org/TLD_List



    # from http://stackoverflow.com/a/10473026/723139
    function endsWith($haystack, $needle)
    {   
        $haystack = strtolower($haystack);
        $needle = strtolower($needle);
        return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
    }
    foreach($valid_tlds as $tld){
        if(endsWith($url, $tld))
        {
            echo substr($url, 0, -strlen($tld)) . $replacement_tld . "\n";
            break;
        }
    }
?>
thewheat
  • 988
  • 7
  • 13
  • Yes this is very close to what I am looking for! Specifically, if there is a way to have a list of valid TLDs already. I was hoping there would be an inbuilt function to check if the given TLD is valid, and if yes, use string functions to convert that TLD to `.com` and store. – Sainath Krishnan Jun 02 '14 at 10:55
  • Not sure if there is a canonical list especially since they can add valid TLDs in the future but perhaps you can use the following. Not ideal but will work. You would also have to ensure $tld isn't empty `$valid_tlds_source = file_get_contents("http://data.iana.org/TLD/tlds-alpha-by-domain.txt");` `$valid_tlds = explode("\n", $valid_tlds_source);` – thewheat Jun 02 '14 at 11:02
0

The question is not entirely clear, I'm assuming you wish to make this logic on PHP part.

Here's useful function to parse such strings:

function parseUrl ( $url )
{
    $r = "^(?:(?P<scheme>\w+)://)?";
    $r .= "(?:(?P<login>\w+):(?P<pass>\w+)@)?";
    $r .= "(?P<host>(?:(?P<subdomain>[\w\.\-]+)\.)?" . "(?P<domain>\w+\.(?P<extension>\w+)))";
    $r .= "(?::(?P<port>\d+))?";
    $r .= "(?P<path>[\w/]*/(?P<file>\w+(?:\.\w+)?)?)?";
    $r .= "(?:\?(?P<arg>[\w=&]+))?";
    $r .= "(?:#(?P<anchor>\w+))?";
    $r = "!$r!";

    preg_match( $r, $url, $out );

    return $out;
}

You can parse URL, validate it, and then recreate from resulting array replacing anything you want.

If you want to practice regexp and create own patterns - this site will be best place to do it.

If your goal to route users from one url to another or change URI style, then you need to use mod rewrite.

Actually in this case you will end up configuring your web server, probably virtual host, because it will route only listed domains (those being parked at the server).

h.s.o.b.s
  • 1,299
  • 9
  • 18
0
  1. Create an empty text file using a text editor such as notepad, and save it as htaccess.txt.

301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "mt-example.com" domain:

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://mt-example.com/

302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:

# This allows you to redirect your entire website to any other domain
Redirect 302 / http://mt-example.com/

For more details : http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F

Fero
  • 12,969
  • 46
  • 116
  • 157
0

To validate a URL in PHP You can use filter_var() .

filter_var($url, FILTER_VALIDATE_URL))

and then to get Top Level Domain (TLD) and replace the it with .com , you can use following function :

 $url="http://www.dslreports.in";
 $ext="com";
 function change_url($url,$ext)
 {
    if(filter_var($url, FILTER_VALIDATE_URL)) {
    $tld = '';
    $url_parts = parse_url( (string) $url );
    if( is_array( $url_parts ) && isset( $url_parts[ 'host' ] ) )
      {
        $host_parts = explode( '.', $url_parts[ 'host' ] );
        if( is_array( $host_parts ) && count( $host_parts ) > 0 )
           {
          $tld = array_pop( $host_parts );
           }
       }

        $new_url=  str_replace($tld,$ext,$url);
        return $new_url;
   }else{
    return "Not a valid URl";
   }
}

 echo change_url($url,$ext);

Hope this helps!

zafus_coder
  • 4,451
  • 2
  • 12
  • 13