SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Mr Toner's Avatar
    Join Date
    Sep 2003
    Location
    Southern California, USA
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to know if URL is routable?

    Hey, folks! I run a multi-site installation of Drupal -- what this means is that the domains running Drupal all point to the same installation, but use separate settings files.

    The settings files are located in the /public_html/directory, in subdirectories corresponding to the domain; for example:

    example.com
    example2.com
    blog.example3.com
    example.com.subdir

    As the last two show, Drupal can supply unique settings at the subdomain and subdirectory (example.com/subdir) level.

    Part of the setup of Drupal includes configuring cron to execute cron.php on an hourly schedule. For a multi-site installation, it makes sense to use a single script to run cron.php for each domain:

    cron-all.pl
    PHP Code:
     #!/usr/bin/perl -w

    use strict;

    my $sites_dir "/path/to/my/drupal/sites";
    my @sites;

    system("lynx -source http://example.com/cron.php\n");

    if(
    opendir(SDIR$sites_dir)) {
      @
    sites grep { /^[^\.]/ && -d $sites_dir."/".$_ readdir(SDIR);
      for(
    my $c=0;$c<$#sites+1;$c++) {
        
    if (($sites[$cne 'all') && ($sites[$cne 'default')) {
          
    system("lynx -source http://".$sites[$c]."/cron.php\n");
        }
      }
      
    closedir(SDIR);
    } else {
      print(
    "Could not open sites dir: ".$sites_dir."\n");
    }

    exit(
    0); 
    (The "all" and "default" directories are ignored in the sites directory.)

    A problem occurs when the site is in a subdirectory: example.com.subdir obviously isn't going to resolve. Does anyone have any suggestions for determining if $sites[c] is routable and, if not, stripping off the last segment and using it as a directory? TIA!


    Don

  2. #2
    SitePoint Addict mmanders's Avatar
    Join Date
    Jul 2006
    Location
    Edinburgh, Scotland
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would use a regex to determine if the URL is valid, i.e. does it contain something random like ".subdir" at the end. If this is the case, then you can use the rindex function to return the index of the last e.g. '.' in your URL string. This index can then be used with substr using the negative value of rindex as the offset to return everything in the string up until that point.

  3. #3
    SitePoint Enthusiast Mr Toner's Avatar
    Join Date
    Sep 2003
    Location
    Southern California, USA
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the starting point, Max. I actually rewrote it in PHP, stripped off successive elements of the string, and used gethostbyname() to identify a valid domain.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •