将绝对路径转换为网址

时间:2018-06-20 13:03:27

标签: coldfusion

我有一个路径:c:\ home \ example.com \ wwwroot \ img \

我需要将其转换为网址:https://example.com/img/

问题是路径是动态的,因此url是动态的。我在变量中有路径。我不在路径中的页面上,否则可以轻松使用ExpandPath()。

我所拥有的是我这样获得的根网址:

<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#">

这给了我根:https://example.com

我被困在那里。

因此,如果路径为“ c:\ home \ example.com \ wwwroot \ folder \ sub-folder \”

我需要将其转换为网址:https://example.com/folder/sub-folder/

2 个答案:

答案 0 :(得分:0)

怎么样:

<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#>

基本上,您需要从绝对路径中删除“ c:\ home \”和“ wwwroot \”,并将\转换为/。这只是做到这一点的一种方法。

答案 1 :(得分:0)

您可以使用这些变量...

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

例如:

<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#'>