4

I have an application I'm building in ColdFusion, whereby all requests will run through the index.cfm file.

I have a .htaccess file that rewrites the URL. So, for example...if I write:

http://domain.com/hello/goodbye/howdy

The actual request always uses index.cfm like so:

http://domain.com/index.cfm/hello/goodbye/howdy

This all works great, but now I'm stuck with how I can grab everything that is in the URL. Not one of the CGI variables don't seem to output the "/hello/goodbye/howdy" part of the URL.

I have tried cgi.path_info and cgi.query_string etc to no avail...they're just blank.

I need to grab everything that comes after the domain name, and do stuff in CF with it. I know it's possible in JS, but I really need this on the server.

Dumping the CGI scope shows me nothing useful in this regard:

<cfdump var="#cgi#" />

Here's my htaccess file for reference:

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /
RewriteRule ^index\.cfm$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.cfm [L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.domain\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

</IfModule>

Thanks.

EDIT:

As an additional note, I've also tried the underlying Java methods like so:

<cfdump var="#getPageContext().getRequest().getContextPath()#" />
<cfdump var="#getPageContext().getRequest().getRequestURL()#" />
<cfdump var="#getPageContext().getRequest().getQueryString()#" />

To no success :(

Michael Giovanni Pumo
  • 14,338
  • 18
  • 91
  • 140
  • That's strange - `QUERY_STRING` is usually the variable that contains exactly that info. (At least in a normal Apache install.) – Pekka Feb 15 '12 at 11:13
  • Yes, it is odd isn't it. I'm really not clued up enough on this stuff to really understand it. If I do a "normal" URL like www.mydomain.com/?/hello/how/can/i/help then it seems to work. Obviously the "?" is the trigger here. But my .htaccess file should negate the required use of that. – Michael Giovanni Pumo Feb 15 '12 at 11:30
  • aggh, as Jura says below, it's `REQUEST_URI` of course, not `QUERY_STRING`. QUERY_STRING contains only the part after the ? by design. Sorry – Pekka Feb 15 '12 at 11:33

4 Answers4

5
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteRule ^index\.cfm$ - [L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.domain\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

#Change exists here:
RewriteRule ^(.*)$ /index.cfm?actualuri=$1 [L,QSA]

</IfModule>

try cgi.query_string now. It should have actualuri=/the/path/sent.
Also, put the rewrite rules in the same order as put above.

ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
3

Check #CGI.REQUEST_URI# - it's undocumented but works

Jura Khrapunov
  • 1,024
  • 6
  • 14
  • Are you sure? According to the docs, it does work: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html Technically it IS documented. Remember that the CGI scope in CF is driven partially by the web server. Since different web servers send different values, the cfdump of the CGI scope will not always display everything you can use. Heck, you can even output cgi.ray (try it). – Raymond Camden Feb 15 '12 at 11:43
  • Hi Ray, I feel you are possibly correct in this sense. Another realization that CGI variables are perhaps best not to be relied upon. However, I sorted this with ThinkingMonkey's .htaccess modification. Works like a charm. PS Ray...big fan of your blog / work! – Michael Giovanni Pumo Feb 15 '12 at 11:56
2

I hope this is what you are looking for.

  <cfset link = "http://" & GetHttpRequestData().headers['host'] & GetHttpRequestData().headers['X-REWRITE-URL'] >
Vikrant Shitole
  • 506
  • 10
  • 19
0

I think the simplest means is to actually look at the CGI.PATH_INFO field.

Jake Feasel
  • 16,785
  • 5
  • 53
  • 66
  • But you wont get query string and cgi gives you actual path not the copy of url. There is one more solution if you are using java script 'document.URL' – Vikrant Shitole Aug 16 '13 at 20:59