With Coldfusion, how do you handle dynamically generated URLs?

(Update: I converted this question to a community wiki, as the answer seems more subjective than I thought. It will be a few answers depending on needs.)

If I have a folder that only includes application.cfc and index.cfm, what is a quick, reliable method for handling dynamically generated URLs? that is, URLs that do not have a corresponding physical .cfm file.

This url example generates 404, but it should look for the page in db and return it via index.cfm:

http://www.myserver.com/cfdemo/mynewpage.cfm

Should I use onMissingTemplate () in application.cfc to handle the missing file? Since this method does not handle onRequestStart (), onRequest (), and onRequestEnd (), I wonder if it should be avoided.

Alternatively, I can configure the ISAPIRewrite rule, since I use IIS (or mod_rewrite on Apache)

# IF the request is not /index.cfm, doesn't exist and ends in cfm or html,
# rewrite it. Pass the requested filename $1.$2 as the 1st param: cgi.page
# append the remaining url params $4 ($3 is the ?)
RewriteCond %{SCRIPT_NAME} ^(?!/index.cfm)(.*)$
RewriteCond %{REQUEST_FILENAME}     !-f
RewriteCond %{REQUEST_FILENAME}     !-d 
RewriteRule ^\/(.*)\.(cfm|html)(\??)(.*)$   /index.cfm?page=$1.$2&$4 [I,L]

Are these methods suitable, or am I missing the best way to achieve this? It seems that Coldfusion should have such a function built into application.cfc. Maybe I just missed it.

+3
source share
5 answers

nothing wrong with rewriting URLs at the web server level. I voted for it.

+4
source

CF cfm/cfc, Application.cfc - :

<cfif Right(cgi.SCRIPT_NAME, 9) NEQ "index.cfm">
    <!--- analyze the SCRIPT_NAME and start processing --->
</cfif>

, -, , . 404. , IIS cgi.QUERY_STRING, , 404.cfm ( ) :

<!--- trap 404 requests triggered by IIS --->
<cfif right(cgi.SCRIPT_NAME, 7) EQ "404.cfm">
    <cflog file="mylogfile" text="404 error triggered by IIS. Context: #cgi.QUERY_STRING#">
</cfif>

Apache , , :

ErrorDocument 404 /404.cfm
+2

URL SES, .

-, . , Google , URL- .

-: CF SES- hostname/file.cfm/param1/param2. , Ray Camden BlogCFC, . CF8, CF7. , , Google ( Bing - ).

+1

, URL-, :

http://www.myserver.com/cfdemo/mynewpage.cfm

http://www.myserver.com/cfdemo/mynewpage OR
http://www.myserver.com/index.cfm/cfdemo/mynewpage

onRequest. -, Apache IIS. ColdFusion. : http://www.cfcdeveloper.com/index.cfm/2007/4/7/Coldfusion-SES-URL.

, .cfm , URL- Apache IIS, , cfm , , onMissingTemplate. , onRequest, .

0

URL. , , CF. , , CF , onapplicationstart, onrequeststart .

URL-, /index.cfm/foo/bar/, unpro . , URL- (,/foo/bar), , , ( , ), . , : "-, . Google, , , URL- ". , , .

0

Source: https://habr.com/ru/post/1725874/


All Articles