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

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.

Related

Coldfusion per application mapping randomly fails

In my application.cfc file I have the following code:
this.mappings["/includes"] = "#expandPath('../../priv/inc/')#";
and then on a page I have the following CF include set up:
<cfinclude template="/includes/imageOptimise-thumbnail.cfm">
For the majority of the time when the page is submitted the imageOptimise-thumbnail page runs without any issue but occasionally it will error saying that it can't find the imageOptimise-thumbnail.cfm. If I resubmit the page, chances are it will work but the question is, why is it sometimes not being able to find the page?
I had a look in the CF Admin and I can't see anything in the logs saying that there was an error but it is clearly showing a Coldfusion 'template not found' error message when it fails.
Anyone got any ideas?
I think your issues stem from the fact that the path used for the mapping is relative.
According to advice in ColdFusion Mappings and Relative Paths,
the moral of the story is to NEVER use expandPath() to create a
mapping that’s relative to the webroot.
Try to make the webroot static as the blog suggests
<cfset this.mappings["/myapplication"] =getDirectoryFromPath(getCurrentTemplatePath())
and navigate to /priv/inc/ from the webroot

Coldfusion mapping cfinclude

I am trying to use mappings for the first time and I am having some issues. In my CFadmin I created a mapping which goes like this:
Logical Path:
/email_sender
Directory Path:
E:\sites\Example.Com\cf_modules\autoresponders\Emails\emailLists
I am trying to access the include file from a .cfc. Inside my cfc I have an include file which is in the same folder and it works just fine but that include file is tying to access another include file based on the information I am passing through.
The include file inside the component has this code in it.
<cfmail from="#emailData.sender#" to="#surveymain.email#" subject="#subject#" type="HTML" >
<cfinclude template="#emailData.includePath#" >
</cfmail>
Just to be more clear this is my total filesystem:
access.cfm -> some.cfc -> include_1 (works) -> include_2 (issue with
the path)
I have a try/catch which emails me the errors and it is located inside the include_1.
How can I use the mapping that I created to finally be able to access my include file?
Please note that after emailLists I have dynamic folders which change depending on which client I am trying to send the email to. So the end result of the path would be:
E:\sites\Example.Com\cf_modules\autoresponders\Emails\emailLists\client_A\email_template.cfm
I tried using my mapping by doing this
<cfinclude template="/email_sender/#emailData.includePath#" >
which gave me this error:
The path to the CFC must be specified as a full path, or as a relative
path from the current template, without the use of mappings.
Also, I tried including a full path in the cfinclude whic also resulted in the following error:
Note: If you wish to use an absolute template path (for example, >template="/mypath/index.cfm") with CFINCLUDE, you must create a
mapping for the path using the ColdFusion Administrator. Or, you can
use per-application settings to specify mappings specific to this
application by specifying a mappings struct to THIS.mappings in
Application.cfc. Using relative paths (for example,
template="index.cfm" or template="../index.cfm") does not require the
creation of any special mappings. It is therefore recommended that you
use relative paths with CFINCLUDE whenever possible.
Any help/suggestion would be much appreciated
Edit:
After restarting the server the version that worked was this one:
<cfinclude template="/email_sender/#emailData.includePath#" >
Thanks to those who replied.
Some questions, rather than some answers.
Did you set the mapping in CFAdmin or in Application.cfc?
What do you get if you run the code below?
#expandPath("/email_sender")#
What is the value of #emailData.includePath#?
What is the value of this: #expandPath("/email_sender#emailData.includePath#")#?
Can you provide us with the exact text of the error messages, rather than vaguely describing them.
What is the value of #fileExists(expandPath("/email_sender#emailData.includePath#"))#?
If you can update your question with that info, you'll either spot where you're going wrong, or we can help to work out what the story is
Something like this happened to me once before. Essentially we edited the xml file, rather than using the admin interface. It wasn't until our sys admin restarted the server until we realized why the mapping hadn't taken. So have you restarted the server since you added the mapping?

Is there a "clean URL" (mod_rewrite) equivalent for iPlanet?

I'm working with Coldfusion (because I have to) and we use iPlanet 7 (because we have to), and I would like to pass clean URL's instead of the query-param junk (for numerous reasons). My problem is I don't have access to the overall obj.conf file, and was wondering if there were .htaccess equivalents I could pass on the fly per directory. Currently I am using Application.cfc to force the server to look at index.cfm in root before loading the requested page, but this requires a .cfm file is passed, so it just 404's out if the user provides /path/to/file but no extension. Ultimately, I would like to allow the user to pass domain.com/path/to/file but serve domain.com/index.cfm?q1=path&q2=to&q3=file. Any ideas?
You can mod_dir with the DirectoryIndex directive to set which page is served on /directory/ requests.
http://httpd.apache.org/docs/2.2/mod/mod_dir.html
I'm not sure what exists for iPlanet, haven't had to work with it before. But it would be possible to use a url like index.cfm/path/to/file, and pull the extra path information via the cgi.path_info variable. Not exactly what you're looking for, but cleaner that query-params.

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.

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

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.