开发者

Parse Entire URL in ColdFusion

I am trying to figure out how to parse an entire URL in ColdFusion.

By entire URL I mean something link this - http://subdomain.domainname.com/crm/leads/view/

I can figure out the first part by using the CGI.HTTP_HOST which gives me - subdomain.domainname.com. I cannot figure out h开发者_运维问答ow to read the - crm/leads/view/ part of the URL.

Is there a variable I can use to read that? I found some UDFs that will parse a full string like that for me but I need to be able to pass it the full URL.

I am also using URL Rewriting so that complicates it some. The URL could be http://subdomain.domainname.com/crm/leads/view/ but the actual page that is serving is http://subdomain.domainname.com/public/index.cfm.

I know this is the way many Frameworks work where all URLs are routed to a file and then it some how parses the URL and directs it to a certain controller and action.

Here is a copy of my .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.cfm [NC,L]

Any feedback on this would be great.

Thanks!


Ok, I'm going to make a few assumptions based on the SES URLs above, what you're trying to do, and how to work it into CGI.PATH_INFO below.

Root directory, .htaccess file:

RewriteEngine On
RewriteCond %{THE_REQUEST} /crm/([^?\ ]+) 
RewriteRule ^.*$ /public/index.cfm/%1 [NC,L]

/public directory. .htaccess file:

RewriteEngine Off

This will redirect into CGI.PATH_INFO, for parsing. Read on for how to parse that.

When you are using Search-Engine Safe (SES) URLs, where your keys/values are all delimited by forward slashes, ColdFusion will consider these in the CGI.PATH_INFO server variable, usually reserved for directories.

So, knowing that, use a simple extraction mechanism to parse it:

<cfset SESQueryString = CGI.PATH_INFO />

<cfset num_pairs = ListLen(SESQueryString,'/') />

<cfset keyVals = StructNew() />

<cfloop from="1" to="#num_pairs#" step="2" index="i">
     <cfset keyVals[ListGetAt(SESQueryString,i,'/')] = ListGetAt(SESQueryString,i+1,'/') />
</cfloop>

<cfdump var=#keyVals#>

Keep in mind that this answer assumes you have an even number of key/val matches in CGI.PATH_INFO. Also remember that List* functions in CF will (in many cases) will throw away an empty list value, so you may think you have an even number, you actually do not.

Scan the list functions on Adobe LiveDocs or cfquickdocs.com to see if the list function you are working with has a parameter you can pass it that will cause it to force empty list values in your list to not be tossed away. One such function is ListToArray().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜