There's really not much to this Tutorial, as it happens to be rather straightforward and easy to implement.

seo_urls.cfm:

This is the code that you can include in your Application.cfc file, or at the top of any of your website files, or include it dynamically via the cfinclude tag.

<cfset urlstring = cgi.path_info>
<cfloop from="1" to=#ListLen(urlstring,"/")# index="i">
<cfif i mod 2>
<cfset paramName = "URL." & ListGetAt(urlstring,i,"/")>
<cfelse>
<cfparam name="#paramName#" default="#ListGetAt(urlstring,i,"/")#">
</cfif>
</cfloop>

<cfif cgi.QUERY_STRING contains "=">
<cfset moveUrl = #rereplace (cgi.QUERY_STRING,"[=?&]","/","ALL")#>
<cfset newUrl = 'http://#cgi.path_info# & "/" & #moveUrl#>
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="#newUrl#">
</cfif>

<cfif cgi.QUERY_STRING contains "=">
<cfset moveUrl = #rereplace (cgi.QUERY_STRING,"[=?&]","/","ALL")#>
</cfif>
<cfinvoke component="urlRewrite" method="sefurls" returnvariable="url">
        <cfinvokeargument name="URLList" value="#CGI.PATH_INFO#">
</cfinvoke>

Now what the above code is doing, is reading your URL strings, and changing any =?& to / then setting that as the variable moveurl.
It's also ensuring that nothing was left out, and doublechecking for any unconverted = in the second <cfif> statement.

When that is Completed, it the redirects you to the new url, and invokes the following component:

urlRewrite.cfc:

<cfcomponent>
    <cffunction access="public" name="sefurls" output="true" returntype="struct">
        <cfargument name="URLList" type="string" required="yes" default="" />
        <cfscript>
            LoopIterations = ListLen(URLList, "/") / 2;
        </cfscript>
            <cfloop from="1" to="#LoopIterations#" index="i">
                <cfset tmp = SetVariable("URL.#ListGetAt(URLList, 1, "/")#", "#ListGetAt(URLList, 2, "/")#")>
                <cfset URLList = ListDeleteAt(URLList, 1, "/")>
                <cfset URLList = ListDeleteAt(URLList, 1, "/")>
            </cfloop>
        <cfreturn url>
    </cffunction>
</cfcomponent>

After Implementing this code, all of your urls will be SEO Friendly, and your querystrings will contain /'s instead of =?&'s.

There's also no need to recode any of your links that contains ?var1=1&var2=2 as they'll be dynamically rewritten.

About This Tutorial
Author: James Harvey
Skill Level: Beginner 
 
 
 
Platforms Tested: CFMX,CF8
Total Views: 25,050
Submission Date: September 24, 2009
Last Update Date: September 24, 2009
All Tutorials By This Autor: 12
Discuss This Tutorial
Advertisement

Sponsored By...
Powered By...