Marthinus Strydom
Marthinus Strydom

Reputation: 267

Converting an absolute path into a url

I have a path: c:\home\example.com\wwwroot\img\

I need to convert it into a url: https://example.com/img/

The thing is that the path is dynamic and therefore the url is dynamic. I have the path in a variable. I am NOT on the page in the path, otherwise I would easily be able to use ExpandPath().

What I have is the root url which I get like this:

<cfif isDefined("cgi.SERVER_PORT_SECURE") and cgi.SERVER_PORT_SECURE eq 1> 
  <cfset http_sec = "https://">
<cfelse>
  <cfset http_sec = "http://">
</cfif>
<cfset websiteurl = "#http_sec##cgi.http_host#">

This gives me the root: https://example.com

This where I am stuck.

So if the path is "c:\home\example.com\wwwroot\folder\sub-folder\"

I need to convert it into a url: https://example.com/folder/sub-folder/

Upvotes: 0

Views: 418

Answers (2)

Jules
Jules

Reputation: 2019

You can use these variables...

Protocol = #getPageContext().getRequest().getScheme()#;
Domain = #cgi.server_name#;
Template = #cgi.script_name#;
Variables = #cgi.query_string#;

So for example:

<cfset path = "c:\home\example.com\wwwroot\folder\sub-folder\">
<!--- remove the site loc on disk --->
<cfset path = replaceNoCase(path, 'c:\home\example.com\wwwroot\', '', 'all')>
<!--- convert slashes --->
<cfset path = replace(path, '\', '/', 'all')>
<!--- put it all together --->
<cfset myURL = '#getPageContext().getRequest().getScheme()#://#cgi.server_name#/#path#?#cgi.query_string#'>

Upvotes: 0

Nebu
Nebu

Reputation: 1793

How about:

<Cfset path = "c:\home\example.com\wwwroot\folder\sub-folder\">

<cfif isDefined("cgi.SERVER_PORT_SECURE") and cgi.SERVER_PORT_SECURE eq 1> 
  <cfset http_sec = "https://">
<cfelse>
  <cfset http_sec = "http://">
</cfif>

<cfset weburl = replacenocase(path,'c:\home\','','one')>
<cfset weburl = rereplacenocase(weburl,'(.*?)(wwwroot\\)(.*)','\1\3','one')>
<cfset weburl = replace(weburl,'\','/','all')>

<cfset weburl = "#http_sec##weburl#">

<Cfdump var=#weburl#>

Basically you need to strip the "c:\home\" and "wwwroot\" from the absolute path and convert the \ into a /. This is just one way to do this.

Upvotes: 0

Related Questions