Must one use an MVC FrameW to produce clean URL's? - coldfusion

The host that I want to host with does not support server side url rewriting, thus no third party tools can be installed to rewrite the url's.
This is Coldfusion 8, on windows, IIS.
The other alternative that I know of is to use a framework, but I do not feel like taking that route (time), for the application works well as it is (but the URL).
Can clean urls be generated by purely CF?
I do not need the clean url's for seo, rather it will be for the user's easy reference to their page. E.g. youtube.com/userpage
Any sugessions?
If the only choice is to use a framework, then which one is most compatible with traditional cfml'', cfm's & CFC's? In that there needs to be minimum changes to the code in the conversion from the none frameworked app to become frameworked.
Thanks for you contributions!

No. you do not need a framework or URL rewriter to get http://domain.com/some/url to work (notice no index.cfm).
In IIS you can set up custom error pages for 404 errors. Make the custom error page execute a ColdFusion page on your server (/urlhandler.cfm, 404.cfm or index.cfm for example). Within that page, you can control your own routes with ColdFusion by using list methods on the cgi.query_string value. IIS will provide you a url that looks something like 404;http://domain.com/the/original/url which you can parse to route the visitor to your desired event.
<!--- Get URL String --->
<cfset CurrentURL = ListGetAt(cgi.query_string, 2, ";")>
<cfset CurrentURL = Replace(CurrentURL, ":80", "")>
<cfset CurrentURL = Replace(CurrentURL, ":443", "")>
<cfset CurrentURL = Replace(CurrentURL, "403;", "")>
<cfset CurrentURL = Replace(CurrentURL, "'", "", "ALL")>
We have a site that receives approx a million visitors a month that is still running SES urls with this method. I was shocked when I was hired and found this existing code at the heart of the site and would not elect to repeat it, but, if you have limitations on installing a rewriter or third party framework (this client placed restrictions on the site) this solution may work for you.
By playing with the above code, you can quickly see how you may use CF to dynamically include the .CFM file you want or execute the right CFC code depending on your set up.

You can use the Framework/1 framework or CFWheels to achieve clean URL's but it will need to include the "/index.cfm/" at the beginning of the URL in order to trigger ColdFusion's application handler code.
EDIT: Please see Aaron Greenlee's work around to prevent the "index.cfm" from appearing in the URL.
i.e. Whichever approach you take, if you cannot add a 3rd party tool to rewrite URLs (and not using Apache), your URL's will be in the form of http://site.com/index.cfm/section/item
eg.
http://site.com/index.cfm/user/login
http://site.cfm/index.cfm/user/signup
FW/1 offers the option of passing in URL variables in a search engine friendly format as well.
Examples:
http://site.com/index.cfm/user/login/email/me#you.com/password/test
is the same as
http://site.com/index.cfm/user/login?email=me#you.com&password=test
is the same as
http://site.com/index.cfm?action=user.login&email=me#you.com&password=test

Go ahead and learn a framework. Most will work for this. However if you just do not want to learn a framework.
www.mysite.com/products/
will run:
www.mysite.com/products/index.cfm
www.mysite.com/products/books
will run:
www.mysite.com/products/books/index.cfm
Framework/1 and CFZen will work for this a they are very simple 1 file frameworks that you can just work around.
CFZen
http://cfzen.riaforge.org
http://cftipsplus.com/blog/?tag=cfzen
Framework/1
http://fw1.riaforge.org
http://corfield.org - the author of Framework/1

Instead of using http://yoursite.com/index.cfm/user, you can do http://yoursite.com/user.cfm and catch the error in the OnMissingTemplate function of application.cfc. This will not require you to set a custom 404 page.

Related

How to get directory path up to a specific folder only?

Using: CF10 and IIS7.5
I have a section within my website called "Bookings". It is located like this:
c:\inetpub\wwwroot\mysite\bookings
Within this folder will be sub-folders and eventually webpages themselves. Heres an example:
c:\inetpub\wwwroot\mysite\bookings\holidays\new.cfm
c:\inetpub\wwwroot\mysite\bookings\carhire\edit.cfm
I include (using <cfinclude>) another page within each webpage that displays different links depending on which page is calling it. All I want to know is the directory up to the "bookings" folder. Something like this (pseudo code):
<cfset whereAmI = #GetDirectoryFromPath(GetBaseTemplatePath())#>
<cfif #whereAmI# EQ "C:\inetpub\wwwroot\mysite\bookings">
<h1>Booking Section Links</h1>
</cfif>
The above code works only if the user visits the bookings/index.cfm page of the "bookings" folder. But if they go to the bookings/holidays/new.cfm page, it is now in the holidays folder so the <h1> content will not appear. I really only want to check for any page that is in the bookings folder, even if it is within a subfolder within the bookings folder. A bit like in SQL where I could say IF #GetDirectoryFromPath(GetBaseTemplatePath())# LIKE 'c:\inetpub\wwwroot\mysite\bookings%' so it has a wildcard on the end.
I know this question is going to irritate the MVC framework advocates but please excuse me on this!
Here is a quick, easy way to solve your problem (may not work as system expands - but should get you started down the right path).
<cfset whereAmI = GetDirectoryFromPath(GetBaseTemplatePath())>
<cfif whereAmI CONTAINS "C:\inetpub\wwwroot\mysite\bookings">
<h1>Booking Section Links</h1>
</cfif>
Note, I removed the # from inside the cfset and cfif you do not need them there.
You could even scale back the path to use just 'mysite\bookings'.
Ideally, this should be wrapped up into a function so that you can easily pass different paths into it to determine if you are on a given page. Or, possibly, even determine the 'parent' folder in onRequestStart in Application.cfc and set it as a request scope variable.
This will need to be tweaked if you run the code on a *nix based system.
It is more easier with CGI variables. You can use "CF_TEMPLATE_PATH". Try this
<cfoutput>The value of CF_TEMPLATE_PATH is: </cfoutput><cfdump var="#CF_TEMPLATE_PATH#">

How do you acquire the rewritten CFWheels URL of an given page?

CFWheels has the URLFor() function for getting the internal URL based on supplied arguments. Is there a way to get the internal URL without supplying any arguments?
For example:
Given a user navigates to "http://somedomain.com" or "http://somedomain.com/about/" or "http://somedomain.com/contact/" is there a method like ReWrittenURL() that returns something like "/" or "/about/" or "/contact/"?
Using URLFor() with no arguments returns "/home/index" or "/about/index" or "/contact/index".
CGI.SCRIPT_NAME returns "/rewrite.cfm"
Obviously with Javascript using document.location.href I can get what I'm after.
Does CGI.path_info have the value you're looking for?
edit
At first, I deleted this post, being utterly confounded. Now I've done a little test - I downloaded the latest wheels core files (1.1.6), extracted to an IIS 7.5 (with URL Rewrite module installed) + CF9 webserver, and edited the "web.config" file in the core root, setting "enabled='true'" for the rewrite rule. Also, since I was running this example from a subfolder, I changed the path from "/rewrite.cfm" to just "rewrite.cfm". This got me to the point where I was able to successfully requests urls like this:
http://server/wheelstest/wheels/wheels
From here, I edited the layout.cfm under views/wheels, adding:
<cfdump var="#cgi#">
When I then request the above URL (/wheelstest/wheels/wheels), I see the dump for the cgi scope. Under path_info, this is the value: /wheels/wheels.
Next, I added a blank "index.cfm" file under views/wheels.
When I request /wheelstest/wheels, I get this for path_info: "/wheels".
When I request /wheelstest/wheels/, I get this for path_info: "/wheels/".
When I request /wheelstest/wheels/index, I get this for path_info: "/wheels/index".
When I request /wheelstest/wheels/index/, I get this for path_info: "/wheels/index/".
So basically - cgi.path_info is doing for me exactly what you describe you want. What is different about your setup than mine, such that it isn't returning that value for you?
there might be a better way to do this... but here I go anyway
every page gets sent the #params#
<cfdump var="#params#">
<cfoutput>#params.action#/#params.controller#/#params.key#</cfoutput>
<cfabort>
try putting that in a controller and see the results
the problem is that if the objects inside the params object don't exist you get an error. So the path that gets generated needs to check if the struct key exists and edit accordingly.
CGI.Path_Info will give you the desired results. I've been trying different options however they all failed and went into the redirect loop. As soon as I switched CGI.path_info it all started well.

How do I get rid of the ? (question mark) in the URL that identifies the start of the query string?

I am using ColdFusion 9.1.2.
I set up a new web site that parses the query string after the domain name and slash. What is left is the MusicianID and then a string used to help with SEO. The URL looks like this:
http://awesomealbums.info/?1085/jim-croce
http://awesomealbums.info/?1077/james-taylor
When I share it using Facebook, Facebook removes the question mark and encodes it. They can't seem to parse it so they display it as the home page.
These throw an error that I can't control:
http://awesomealbums.info/1085/jim-croce
http://awesomealbums.info/1077/james-taylor
I notice that StackOverlfow and other sites are able to exclude the question mark that starts a query string. I would like to do the same. I, however, can't change any IIS or CF Administrator settings. I need to code the solution. I've tried, but I get IIS telling me they can't find the page.
I want my URLs to look like this:
http://awesomealbums.info/1085/jim-croce // same as above but no ?
http://awesomealbums.info/1077/james-taylor // same as above but no ?
Here's the code that I am using right now to parse the URL and get the MusicianID.
<cfscript>
QString = CGI.QUERY_STRING;
if (QString eq "") {
include "Home.cfm";
} else if (QString eq "WhoAmI") {
include "WhoAmI.cfm";
} else {
IndexOfSlash = Find("/", QString);
if (IndexOfSlash eq 5) {
ThisID = left(QString, 4);
if (isNumeric(ThisID)) {
MusicianID = ThisID;
include "Musician.cfm";
}
} else {
location(url="http://www.awesomealbums.info" addtoken="false");
}
}
How can I alter my site so that the question mark can be removed and the web server doesn't get funky and I can parse out the query string?
The keyword you are looking for is URL rewriting. It has to happen on the web server, since you want to handle all requests in the top-level directory. If your web server is the Apache httpd, you can do it like this:
RewriteEngine on
RewriteRule ^/(\d{4})/([\w-]+)$ /?$1/$2 [L]
or
RewriteRule ^/(\d{4})/([\w-]+)$ /Musician.cfm?MusicianID=$1 [L]
Since you can't modify the web server (as Roland correctly suggests) there is one alternative - use URLs that look like this:
http://awesomealbums.info/index.cfm/1085/jim-croce
Structured this way, the webserver (IIS) will still pass control to your script. Then you can start having CF take over control of the processing. Your CGI.query_string will be empty, but your cgi.path_info variable will contain /1085/jim-croce. You can then start parsing that and handling it as needed.
The answer for IIS is basically the same as Roland mentions in his answer. You need to use URL Rewriting, that is the only way to accomplish what you are looking to do. This is because technically the URL that you want to request, does not exist as a real page or resource on the server, and you need to use URL Rewriting to intercept the page request, map it using regular expressions, then pass it to your application as the query string (page parameters) that you are expecting. So, if you are doing this on a hosted server, contact your host and see if they have something setup or installed on the server for doing URL Rewriting. Most any decent host certainly will.
If you are using IIS7, then info on using the built in URL Rewriting can be found at the link:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/
If you are using an older version of IIS, then you need to install an application on the server that will do this as previous versions of IIS do not have built in support for regular expression based rewriting that you would need to properly map your URLs to the correct parameters on your query string. For older versions of IIS, I've used Helicon IsapiRewrite which you can find at:
http://www.isapirewrite.com/
riding off of jfeasel's answer, you can use ColdCourse:
http://coldcourse.riaforge.org/

What does this URL mean?

http://localhost/students/index.cfm/register?action=studentreg
I did not understand the use of 'register' after index.cfm. Can anyone please help me understand what it could mean? There is a index.cfm file in students folder. Could register be a folder name?
They might be using special commands within their .htaccess files to modify the URL to point to something else.
Things like pointing home.html -> index.php?p=home
ColdFusion will execute index.cfm. It is up to the script to decide what to do with the /register that comes after.
This trick is used to build SEO friendly URL's. For example http://www.ohnuts.com/buy.cfm/bulk-nuts-seeds/almonds/roasted-salted - buy.com uses the /bulk-nuts-seeds/almonds/roasted-salted to determine which page to show.
Whats nice about this is it avoids custom 404 error handlers and URL rewrites. This makes it easier for your application to directly manage the URL's used.
I don't know if it works on all platforms, as I've only used it on IIS.
You want to look into the cgi.PATH_INFO variable, it is populated automatically by CF server when such URL format used.
Better real-life example would look something like this.
I have an URL which I want to make prettier:
http://mybikesite/index.cfm?category=bicycles&manufacturer=cannondale&model=trail-sl-4
I can rewrite it this way:
http://mybikesite/index.cfm/category/bicycles/manufacturer/cannondale/model/trail-sl-4
Our cgi.PATH_INFO value is: /category/bicycles/manufacturer/cannondale/model/trail-sl-4
We can parse it using list functions to get the same data as original URL gives us automatically.
Second part of your URL is plain GET variable, it is pushed into URL scope as usually.
Both formats can be mixed, GET vars may be used for paging or any other secondary stuff.
index.cfm is using either a CFIF IsDefind("register") or a CFIF #cgi.Path_Info# CONTAINS statements to execute a function or perform a logic step.

With Coldfusion, how do you handle dynamicaly generated URLs?

(Update: I converted this question to a community wiki as the answer appears more subjective than I thought it would. There are multiple answers depending on one's needs.)
If I have a folder that only includes application.cfc and index.cfm, what is a fast, reliable method to handle dynamically generated URLs? i.e. URLs that do not have a corresponding physical .cfm file.
This example url generates a 404, but it should lookup a page in a db and return it via index.cfm:
http://www.myserver.com/cfdemo/mynewpage.cfm
Should I use onMissingTemplate() in the application.cfc to handle the missing file? Since this method doesn't process onRequestStart(), onRequest() and onRequestEnd(), I wonder if it should be avoided.
Alternately, I could setup an ISAPIRewrite rule since I'm using 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 appropriate, or am I missing a better way of accomplishing this goal? It seems that Coldfusion should have this type of feature built into the application.cfc. Maybe I'm just missing it.
nothing wrong with url rewrite on web server level. I'd vote for that.
Because CF by default handles only cfm/cfc requests, you can do in the beginning of Application.cfc something like this:
<cfif Right(cgi.SCRIPT_NAME, 9) NEQ "index.cfm">
<!--- analyze the SCRIPT_NAME and start processing --->
</cfif>
For other filetypes using web-server configuration is the only way I can see. But instead of creating rewriting rules you can try to use custom 404 handlers. At least when using IIS you'll be able to get the context in cgi.QUERY_STRING, if set up the dummy page, say 404.cfm (it does not need to exist) and putting following check before previous example:
<!--- 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>
For Apache it is possible to use following handler, but I'm not sure if you can extract the context in this case:
ErrorDocument 404 /404.cfm
If you are doing this for SES URLs, I'd offer two pieces of advice.
The first is that they matter less and less as time goes on. Google, for example, recognizes that URLs need to include query data.
Second: CF can natively handle SES URLs in the form hostname/file.cfm/param1/param2. Ray Camden's BlogCFC, for example, works that way. It is on by default in CF8, but needs to be enabled in CF7. I don't have a lot of information handy on this, but it should be easy to Google (or Bing, or whatever).
If you can allow it, I'd try to convert URLs like:
http://www.myserver.com/cfdemo/mynewpage.cfm
to:
http://www.myserver.com/cfdemo/mynewpage OR
http://www.myserver.com/index.cfm/cfdemo/mynewpage
so that you don't lose the onRequest methods. The first one can be done only at the webserver level, so in Apache or IIS. The second one can be done in just ColdFusion. See this: http://www.cfcdeveloper.com/index.cfm/2007/4/7/Coldfusion-SES-URL.
Otherwise, if you must have the .cfm at the end, you can use a URL rewrite package in Apache or IIS to strip it out and then forward the request to a cfm page or do what you're doing with onMissingTemplate. I'd try to opt for a solution that doesn't involve losing the onRequest methods, but up to you.
I'd definitely go for URL rewriting. Not only will it be a more predictable, yet generalized approach, but it reduces a significant amount of string parsing load from the CF server. Further, it results in CF handling a request to a real file thereby getting you the benefit of onapplicationstart, onrequeststart, and other events.
As an aside, I've personally always found URLs like /index.cfm/foo/bar/ to look unpro and hackish. Additionally, URLs (like /foo/bar) that don't end in either a file extension or trailing slash are technically incorrect (per old-school static site conventions at the very least) and ought to probably be avoided as well. I'd also be curious where Ben Doom gets his assertion that "The first is that they matter less and less as time goes on. Google, for example, recognizes that URLs need to include query data." In my experience I've actually found the exact opposite to be true.