views:

464

answers:

5

Let's say, on a coldfusion site, that the user has navigated to http://www.example.com/sub1/

The server-side code typically used to tell you what URL the user is at, looks like: http://#cgi.server_name##cgi.script_name#?#cgi.query_string#

however, "cgi.script_name" automatically includes the default cfm file for that folder- eg, that code, when parsed and expanded, is going to show us "http://www.example.com/sub1/index.cfm"

So, whether the user is visiting sub1/index.cfm or sub1/, the "cgi.script_name" var is going to include that "index.cfm".

The question is, how does one figure out which URL the user actually visited? This question is mostly for SEO-purposes- It's often preferable to 301 redirect "/index.cfm" to "/" to make sure there's only one URL for any piece of content- Since this is mostly for the benefit of spiders, javascript isn't an appropriate solution in this case. Also, assume one does not have access to isapi_rewrite or mod_rewrite- The question is how to achieve this within coldfusion, specifically.

+2  A: 

Not sure that it is possible using CF only, but you can make the trick using webserver's URL rewriting -- if you're using them, of course.

For Apache it can look this way. Say, we're using following mod_rewrite rule:

RewriteRule ^page/([0-9]+)\/?$ index.cfm?page=$1&noindex=yes [L]

Now when we're trying to access URL http://website.com/page/10/ CGI shows:

QUERY_STRING page=10&noindex=yes

See the idea? Think same thing is possible when using IIS.

Hope this helps.

Sergii
+1  A: 

I do not think this is possible in CF. From my understanding, the webserver (Apache, IIS, etc) determines what default page to show, and requests it from CF. Therefore, CF does not know what the actual called page is.

Sergii is right that you could use URL rewrting to do this. If that is not available to you, you could use the fact that a specific page is given precedence in the list of default pages.

Let's assume that default.htm is the first page in the list of default pages. Write a generic default.htm that automatically forwards to index.cfm (or whatever). If you can adjust the list of defaults, you can have CF do a 301 redirect. If not, you can do a meta-refresh, or JS redirect, or somesuch in an HTML file.

Ben Doom
+5  A: 

I suppose this won't be possible.

If the client requests "GET /", it will be translated by the web server to "GET /{whatever-default-file-exists-fist}" before ColdFusion even gets invoked. (This is necessary for the web server to know that ColdFusion has to be invoked in the first place!)

From ColdFusion's (or any application server's) perspective, the client requested "GET /index.cfm", and that's what you see in #CGI#.

As you've pointed out yourself, it would be possible to make a distinction by using a URL-rewriting tool. Since you specifically excluded that path, I can only say that you're out of luck here.

Tomalak
A: 

I think this is possible.

Using GetHttpRequestData you will have access to all the HTTP headers.

Then the GET header in that should tell you what file the browser is requesting.

Try

<cfdump var="#GetHttpRequestData()#">

to see exactly what you have available to use.

Note - I don't have Coldfusion to hand to verify this.

Edit: Having done some more research it appears that GetHttpRequestData doesn't include the GET header. So this method probably won't work.

I am sure there is a way however - try dumping the CGI scope and see what you have.

DanSingerman
No, doesn't seem possible with a pure CF solution. Neither the CGI scope nor getHttpRequestData() have the client-side URL string.
Al Everett
A: 

If you are able to install ISAPI_rewrite (Assuming you're on IIS) - http://www.helicontech.com/isapi_rewrite/

It will insert a variable x-rewrite-url into the GetHttpRequestData() result structure which will either have / or /index.cfm depending on which URL was visited.

Martin