简体   繁体   中英

Converting Rackspace Cloud Files CDN URLs from HTTP to HTTPS

I have a series of Rackspace Cloud Files CDN URLs stored which reference an HTTP address and I would like to convert them to the HTTPS equivalent.

Rackspace Cloud Files CDN URLs are in the following format:

http://c186397.r97.cf1.rackcdn.com/CloudFiles Akamai.pdf

And the SSL equivalent for this URL would be:

https://c186397.ssl.cf1.rackcdn.com/CloudFiles Akamai.pdf

The changes to the URL are ( source ):

  1. HTTP becomes HTTPS
  2. The second URI segment ('r97' in this example) becomes 'ssl'

The 'r00' part seems to vary in length (as some are 'r6' etc.) so I'm having trouble converting these URLs to HTTPS. Here's the code I have so far:

function rackspace_cloud_http_to_https($url)
{
    //Replace the HTTP part with HTTPS
    $url = str_replace("http", "https", $url, $count = 1);

    //Get the position of the .r00 segment
    $pos = strpos($url, '.r');

    if ($pos === FALSE)
    {
        //Not present in the URL
        return FALSE;
    }

    //Get the .r00 part to replace
    $replace = substr($url, $pos, 4);

    //Replace it with .ssl
    $url = str_replace($replace, ".ssl", $url, $count = 1);

    return $url;
}

This however does not work for URLs where the second segment is of a different length.

Any thoughts appreciated.

I know this is old, but if you are using this library: https://github.com/rackspace/php-opencloud you can use the getPublicUrl() method on the object, you just need to use the following namespace

use OpenCloud\ObjectStore\Constants as Constant;

// Code logic to upload file
$https_file_url = $response->getPublicUrl(Constant\UrlType::SSL);

Try this:

function rackspace_cloud_http_to_https($url)
{
    $urlparts = explode('.', $url);

    // check for presence of 'r' segment
    if (preg_match('/r\d+/', $urlparts[1]))
    {
        // replace appropriate segments of url
        $urlparts[0] = str_replace("http", "https", $urlparts[0]);
        $urlparts[1] = 'ssl';

        // put url back together
        $url = implode('.', $urlparts);
        return $url;
    }
    else
    {
        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM