SES url in coldfusion - No MVC
Go开发者_JS百科t this url
http://localhost:8500/users.cfm?userid=John
that loads the user's profile by getting the users details from the db
WHERE userid = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#url.userid#">
How can the same profile be accessed like this:
http://localhost:8500/John
Instead of users having to type "users.cfm?userid=John", they simply type the user id of the person whom they want to view.
On CF9, IIS7
I may need to install http://www.iis.net/download/urlrewrite for the solution, please advice.
Appreciate your help.
Using URLRewrite, have the rewriter set the requested URL (e.g. /John
) as a request header and forward it to a single .cfm file (i.e. a front controller). In the .cfm file (e.g. frontcontroller.cfm) read out the request header (i.e. GetHttpRequestData().headers
) and process accordingly -> users.cfm?userid=john
.
Off-hand, maybe you could use the OnRequest method in Application.cfc. That method allows you to filter requests and give them special processing.
http://www.bennadel.com/blog/805-ColdFusion-Application-cfc-OnRequest-Creates-A-Component-Mixin.htm
In your example, you could take "John", or any string, if it exists at the root, and
<cfinclude template="#application.baseHREF#/users.cfm?userid=#userID#" />
If you define baseHREF in OnApplicationStart.
If you decide to use a tool like ISAPI Rewrite (not free) which allows you to write Rewrite rules similar to those used in Apache, you'd add:
RewriteRule ^(.*)$ /users.cfm?userid=$1 [NC,L,QSA]
You can also add folders or other URLs to exclude from this rule with something like:
RewriteCond %{REQUEST_URI} !^(assets|images|xml|tasks) [NC]
RewriteRule ^_admin/(.*)$ /_admin/index.cfm/$1 [NC,L,QSA]
I would recommend using a rewrite rule over handling it in CFML to reduce the processing handled by the CF server.
精彩评论