删除 URL 中的文件名

Stripping Out the File Name in the URL

我正在制作我已经制作了一周左右的脚本的最终版本。除了最后一个变量 finalScriptPath 映射到我的 table columnscriptPath 之外,我的一切都按照我的喜好工作将页面名称从 URL 路径中剥离出来,这样路径就像 "www.example.com/products/2015/example.cfm will render just the "products15" 文件夹结构。请参阅包含的片段。 到目前为止,这是我的代码:

!---Function named EndRequestFunc for testing safely on CF Server--->
<!---Rename function to OnRequestStart and place in Application.cfm file before running live--->
<cffunction name="EndRequestFunc" access="public" returnType="string">
<!---Variable declared and set to empty--->
<cfset referer_path_and_file = "">
<cfset referer_path = "">
<cfset referer_file_name = "">
<cfset script_path_and_file = "">
<cfset script_path = "">
<cfset script_file_name = "">

<cfif cgi.HTTP_REFERER neq ''>
  <!--- all of this will fail if there is no referer, for instance, if they bookmark the page --->
  <!--- cgi.HTTP_REFERER may contain URL parameters, so let's strip those --->
  <cfset referer_path_and_file = ListFirst(CGI.HTTP_REFERER, "?")>
  <!--- now let's get just the path, stripping out the web server info --->
  <cfset referer_path = ListDeleteAt(CGI.HTTP_REFERER, ListLen(CGI.HTTP_REFERER, "/"), "/")>
  <cfset referer_path = ReplaceNoCase(referer_path, "https", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "http", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "://machine1.fss.com", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "://www_dev.fss.com", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "://www.fss.com", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "://10.11.2.60/", "", "All")>
  <cfset referer_path = referer_path & "/">
  <cfset referer_path = ReplaceNoCase(referer_path, "/", "\", "All")>
  <!--- now let's remove everything but the file name --->
  <cfset referer_file_name = ListLast(referer_path_and_file, "/")>
  <!--- and that leaves us with these variables set --->
 <!--- referer_path_and_file = "#referer_path_and_file#"<br />
  referer_path = "#referer_path#"<br />
  referer_file_name = "#referer_file_name#"<br />
  <br />--->
</cfif>

<!--- cgi.SCRIPT_NAME does not include URL parameters --->
<cfset script_path_and_file = ListFirst(CGI.SCRIPT_NAME, "\")>
<!--- now let's get just the path, stripping out the web server info --->
<cfset script_path = GetDirectoryFromPath(GetCurrentTemplatePath())>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\Clients\fss.com\www_dev", "", "All")>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\Clients\fss.com\www", "", "All")>
<!--- now let's remove everything but the file name --->
<cfset script_file_name = ListLast(script_path_and_file, "/")>
<!--- and that leaves us with these variables set--->
<!---script_path_and_file = "#script_path_and_file#"<br />
script_path = "#script_path#"<br />
script_file_name = "#script_file_name#"<br />--->
<!---<!---CFC Test Query--->
   <cfquery name="qryTest" datasource="Fss_Dev" >
INSERT INTO tblCFMPageRequest(pageName)
     VALUES
           ('MyPage.cfm')
   </cfquery>--->

<!---Directory Stripping And Modifier Block Goes Here--->
<!---Set CGI System Variables--->
<cfset currentHeader = CGI.HTTP_REFERER >
<cfset currentScriptPage = CGI.SCRIPT_NAME > 
<!---Set currentScriptPage as command line directory string and delcare new variable "reverseScriptPage"--->
<cfset reverseScriptPage = ReReplace(#currentScriptPage#, "/", "\","ALL")>
<!---Set reverseScriptPage value as newly format command line directory structure--->
<cfset newScriptPage = ListSetAt(#reverseScriptPage#, 1, "#reverseScriptPage#") >
<cfset lastScriptPage = ListFirst(#newScriptPage#, "/") > <!---Added this 5/11/2015--->
 <cfset  finalScriptPage = ListSetAt(#lastScriptPage#, 1, "#lastScriptPage#") >
<cfset finalScriptPath = ReReplace(#finalScriptPage#, "/", "\","ALL") >


<!---CGI.HTTP_HEADER Formatting Block--->
<!---This block retrieves the http header value, then strips http formatting--->
<!---currentHeader variable truncated by ListLast then assigned to variable headerFileName--->
<cfset headerFileName = ListLast(#currentHeader#,"/")>
<!---currentHeader URL string has "/" replaced with "\" characters then set as variable reverseHeaderURL--->
<cfset reverseHeaderURL = ReReplace(#currentHeader#, "/", "\","ALL")>
<!---reverseHeaderURL is stripped of all known http type formattings and IP formattings--->
<cfif cgi.HTTP_REFERER neq ''>
<cfset reverseHeaderURL = ListDeleteAt(cgi.HTTP_REFERER, ListLen(cgi.HTTP_REFERER, "/"), "/")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "https", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "http", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://machine1.fss.com", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://www_dev.fss.com", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://www.fss.com", "", "All") >
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://machine1.fss.com", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://10.11.2.60/", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, ":\dev.fss.com\", "", "All")> <!---Added this line 5/11/2015--->
  <!---The end result is then formatted to C:\ command line format and stored in new variable newHeaderURL--->
 <cfset newHeaderURL = ListSetAt(#reverseHeaderURL#, 1, "#reverseHeaderURL#") >
<cfset finalHeaderURL = ReReplace(#newHeaderURL#, "/", "\","ALL")>
</cfif>

   <!---Queries Table To Get Requested Record--->
    <cfquery name="qryGetPageRecord" datasource="Fss_Dev" dbname="Fss_Dev">
    SELECT referrerPage
    FROM tblCFMPageRequest
    WHERE referrerPage = '#referer_file_name#' AND scriptName = '#script_file_name#'
    </cfquery>
    <!---Conditional Check for record count equal to 0--->
    <cfif qryGetPageRecord.recordCount eq 0>
    <!---<cfset httpReferer = ListLast('http://www.fss.com/index.cfm?uid=testValue',"/")>
    <cfset ListFirst(httpReferer,"?")>--->
    <!---variable declared as scriptName and set equal to CGI.SCRIPT_NAME --->
    <!---Value truncated to actual file name through function ListLast() --->
    <cfquery name="setNewRecord" datasource="Fss_Dev" dbname="Fss_Dev">
    INSERT INTO tblCFMPageRequest (referrerPage, referrerPath, scriptName, scriptPath)
    VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#referer_file_name#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#finalHeaderURL#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#script_file_name#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#finalScriptPath#">)
  </cfquery>
    </cfif>
</cffunction>

经过反复试验,特别是使用代码块,找到了我的解决方案。下面是去除文件名的解决方案,只留下路径。

<cfif cgi.PATH_INFO neq ''>
  <cfset script_path_and_file = ListFirst(CGI.PATH_INFO, "?")>
 <cfset script_path = ListDeleteAt(CGI.PATH_INFO, ListLen(CGI.PATH_INFO, "/"), "/")>
  <cfset script_path = ReplaceNoCase(script_path, "https", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "http", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "://machine1.fss.com", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "://www_dev.fss.com", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "://www.fss.com", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "://10.11.2.60/", "", "All")>
  <cfset script_path = script_path & "/">
  <cfset script_path = ReplaceNoCase(script_path, "/", "\", "All")>
<cfset script_file_name = ListLast(script_path_and_file, "/") >
</cfif>

<cfset script_path = GetDirectoryFromPath(GetCurrentTemplatePath())>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\Clients\fss.com\www_dev", "", "All")>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\Clients\fss.com\www", "", "All")>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\", "", "All")>
<cfset script_file_name = ListLast(script_path_and_file, "/")>

    <cfquery name="setNewRecord" datasource="Fss_Main_Dev" dbname="Fss_Dev">
    INSERT INTO tblCFMPageRequest (referrerPage, referrerPath, scriptName, scriptPath)
    VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#referer_file_name#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#finalHeaderURL#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#script_file_name#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#script_path#">)
  </cfquery>

</cffunction>