Is it possible to rewrite url using ColdFusion?

I have a requirement for creating friendly urls. I am in IIS.

My dynamic urls look like

www.testsite.com/blog/article.cfm?articleid=4432

The client wants the URLs to look like

www.testsite.com/blog/article_title

I know this can be easily done using the rewiter 2.0 IIS URL.

But the client only wants to do this with ColdFusion. The basic idea that he gave

  • User hit URL www.testsite.com/blog/article_title
  • I need to get the article id using article_title in the url.
  • Using the identifier to call article.cfm and load the output into cfsavecontent, and then deliver that output to the browser.

But I do not think that this is possible at the application server level. How IIS Will Understand Our User Friendly URLs OR am I missing something important? Can this be done using ColdFusion at the application server level?

+4
source share
3 answers

Firstly, I do not recommend reinventing the wheel. Web servers do this and do it well.

Cold Fusion can do something similar with #cgi.path_info#. You can jump over some hoops, as Adam Tuttle explains here: Can I have a “friendly” URL without rewriting the URLs in IIS? .


Option # 2: My Favorite: OnMissingTemplate ..

Application.cfc( , .cfm onMissingTemplate).

application.cfc, "" URL- .

<cffunction name="onMissingTemplate">
        <cfargument name="targetPage" type="string" required=true/>
        <!--- Use a try block to catch errors. --->
        <cftry>
                <cfset local.pagename = listlast(cgi.script_name,"/")>
                <cfswitch expression="#listfirst(cgi.script_name,"/")#">
                    <cfcase value="blog">
                        <cfinclude template="mt_blog.cfm">
                        <cfreturn true />
                    </cfcase>
                </cfswitch>
                <cfreturn false />
                <!--- If no match, return false to pass back to default handler. --->
                <cfcatch>
                      <!--- Do some error logging here --->
                      <cfreturn false />
                </cfcatch>
        </cftry>
</cffunction>

mt_blog.cfm , , URL- /blog/How -to-train-your-flea-circus.cfm

<!--- get everything after the slash and before the dot --->
<cfset pagename = listfirst(listlast(cgi.script_name,"/"),".")>

<!--- you may probably cache queries blog posts --->
<cfquery name="getblogpost">
  select bBody,bTitle,bID
    from Blog
   where urlname = <cfqueryparam cfsqltype="cf_sql_varchar" value="#pagename#">
</cfquery>

<!--- This assumes you will have a field, ex: urlname, that has a url-friendly format to match
      to. The trouble is that titles are generically, in most blogs, changing every special char
      to - or _, so it difficult to change them back for this sort of comparison, so an add'l
      db field is probably best. It also makes it a little easier to make sure no two blogs have
      identical (after url-safe-conversion) titles. --->

...

, URL-, /blog/ 173_How-to-train-your-flea-circus.cfm( 173 - )

<!--- get everything after the slash and before the dot --->
<cfset pageID = listfirst(listlast(cgi.script_name,"/"),"_")>

<!--- you may probably cache queries blog posts --->
<cfquery name="getblogpost">
  select bBody,bTitle,bID
    from Blog
   where bID = <cfqueryparam cfsqltype="cf_sql_integer" value="#pageID#">
</cfquery.

...
+3

( CF onMissingTemplate). IIS 404, .

, , , web.config. . "/detail _" + id .

"/blog", . web.config -, - /blog/ URL- /?blogtitle=[everythingAfterBlog]. ( , /blog/article.cfm .)

<rules>
    <rule name="Blog" patternSyntax="ECMAScript" stopProcessing="true">
        <match url="blog/(.*)$" ignoreCase="true" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{SCRIPT_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{PATH_INFO}" pattern="^.*(blog/article.cfm).*$" negate="true" />
        </conditions>
        <action type="Rewrite" url="/?blogtitle={R:1}" appendQueryString="true" />
    </rule>
</rules>

"301 Redirect" URL-, SEO. (-) , (.. ), " ".

+2

cfqueryparam, ColdFusion 404 , 404 - CFM script - . IIS, IIS.

( ), , , . -, HTTP, , . CF , , , . . , - , URL-. , . !:)

+1

All Articles