title: Converting Rackspace Cloud Files CDN URLs from HTTP to HTTPS
body: <p>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.</p>
  
  <p>Rackspace Cloud Files CDN URLs are in the following format:</p>
  
  <p><strong><a href="http://c186397.r97.cf1.rackcdn.com/CloudFiles%20Akamai.pdf" rel="nofollow">http://c186397.r97.cf1.rackcdn.com/CloudFiles Akamai.pdf</a></strong></p>
  
  <p>And the SSL equivalent for this URL would be:</p>
  
  <p><strong><a href="https://c186397.ssl.cf1.rackcdn.com/CloudFiles%20Akamai.pdf" rel="nofollow">https://c186397.ssl.cf1.rackcdn.com/CloudFiles Akamai.pdf</a></strong></p>
  
  <p>The changes to the URL are (<a href="http://www.rackspace.com/blog/rackspace-cloud-files-cdn-launches-ssl-delivery/" rel="nofollow">source</a>):</p>
  
  <ol>
  <li>HTTP becomes HTTPS</li>
  <li>The second URI segment ('r97' in this example) becomes 'ssl'</li>
  </ol>
  
  <p>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:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>This however does not work for URLs where the second segment is of a different length.</p>
  
  <p>Any thoughts appreciated.</p>