When using URL rewrite files are included multiple times? - coldfusion

I am developing a new site in ColdFusion 10 and doing some url rewriting using the .htaccess file. I have three rules that work very well when I call them without other includes. When I wrap them in includes, I get an error.
Here are my rules:
RewriteEngine on
RewriteRule ^advertise$ /index.cfm?Section=Advertise
RewriteRule ^bike/([0-9]+)/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1&FeatureID=$2
RewriteRule ^bike/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1
The index page figures our which section to show and calls includes the appropriate file wrapped around a header and footer, like this:
include "header.cfm";
include "#VARIABLES.Section#.cfm";
include "footer.cfm";
The problem is, it doesn't quite work for the "Bike" pages, the second two rules. It appears that the header file is included several times, which makes the links to the CSS and other files bad, which messes up the page. If I comment out the header and footer includes, everything works just fine, but there's no header or footer, which is not what I want.
// include "header.cfm";
include "#VARIABLES.Section#.cfm";
// include "footer.cfm";
Is there something in the url rewrite rules that causes the bike pages to be loaded several times and then fail?
http://flyingpiston2012-com.securec37.ezhostingserver.com/
You can try the page here and see the problem by clicking the links. Any ideas on how to fix this?
UPDATE
When I make all of my links absolute by including the entire URL, the problem goes away.
<cfoutput>
<link href="#APP.Home#/bootstrap/css/bootstrap.css" rel="stylesheet">
</cfoutput>
Still, I should NOT have to include an absolute path to files. Any hints as to why this isn't working for me?
UPDATE
// THIS FIXES THE PROBLEM
<base href="#APP.Home#/">
<link href="/bootstrap/css/bootstrap.css" rel="stylesheet">
// THIS FIXES THE PROBLEM
<link href="#APP.Home#/bootstrap/css/bootstrap.css" rel="stylesheet">
// THIS IS THE PROBLEM
<link href="/bootstrap/css/bootstrap.css" rel="stylesheet">

Whatever your problem is, trying to make a connection between .htaccess rewrite rules and files included by CF is the wrong troubleshooting approach.
Rewrite rules are applied only to the incoming request: the document requested in the address bar (for example) of the browser.
When one includes a file, it's not making a request, so the webserver (and accordingly the rewrite module) are not involved.
I imagine if you simply browse to the rewritten URL for the bikes page (eg: /index.cfm?Section-Bije&BikeID=12345) you'd see the same problem. This demonstrates the rewriting is not part of the problem.
Factor this notion out of your investigation, and just look at how your files are being included. A good simple place to start is to just turn debugging on, and look at the execution time dispay using the TREE view. This'll give you an idea of where/how/why you're calling in your templates multiple times.

This could happen because you didn't tell Apache to stop parsing on match. Try to make rules look like this:
RewriteRule ^advertise$ /index.cfm?Section=Advertise [L]
RewriteRule ^bike/([0-9]+)/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1&FeatureID=$2 [L]
RewriteRule ^bike/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1 [L]

I don't know if it is related, but I did run into a similar issue with FW/1. By tailing the rewrite.log I could see every cfinclude that did NOT actually exist was actually going through Apache. I added [NS] to the .htaccess and it went away:
# rewrite rules, NS skips rewrite rule for internal sub-requests
# for FW/1, removes index.cfm from the URL
RewriteRule ^(.*)$ /index.cfm/$1 [NS,L]

Related

Rewriting RewriteRule to include back one particular php file in the path

I inherited a large WordPress site: I was looking at the Wordfence live logs when I found users not loading a particular PHP page. I investigated and through an FTP client I found the file was where it was supposed to be. I used some network tool (in Chrome, Opera and Firefox) and, again, I found that file was returning a 404.
So, I found in the root of the website a short htaccess file containing this line:
RewriteRule ^wp-content/(.*)\.php$ [R=404,L]
I commented this out and reloaded the website: no error anymore. I must say, the error apparently doesn't cause anything strange to the website. But I would like to eliminate it.
I suppose this rule is meant to avoid someone can make a direct HTTP request to this and any other PHP file in that directory: in this case I suppose this file I'm talking about is called from an include, not directly, because in WordFence what I see is an error coming after a user accesses directly other pages, not this one in particular.
Anyway, I would like to rewrite this rule so that it stays the same as now, except for that php page. The PHP page is in the path of the theme:
wp-content/themes/themeName/core/css/customized.css.php
Is this possible? Any help is appreciated
If you want to exclude that specific php file from the RewriteRule, you can add a negative lookahead to the regex, like so:
RewriteRule ^wp-content/(?!themes/.*/core/css/customized\.css\.php$)(.*)\.php$ [R=404,L]

How do i change the base url of my application from : http://test.something.com/ to http://test.something.com/abc.cfm

The url : http://test.something.com/ is the login page of the application. By default it calls a page 'login.cfm'. But it doesnt shows in the url. May be it is defined in the server file as a default page.
What I want is, when i access this url [http://test.something.com/], the url should also show the called page i.e. the url should look like : http://test.something.com/login.cfm everytime.
I dumped the cgi variables and below is the stack for the same. I was wondering whether i have to change the cgi.http_referer or cgi.http_host.
Or do I need to change some file on the server side? I am using Apache.
EDITED:
what you are asking might be a browser dependent issue. In the Browser there is option to trim the URL from the address bar, which might be the case for you.
Please check this link and get it confirmed.
http://www.cnet.com/how-to/how-to-show-the-full-url-in-firefox/
Still if you want to change your application's URL to look something else then you can try "URL rewrite"
You can check this link for URL rewrite.
http://www.iis.net/learn/extensions/url-rewrite-module/user-friendly-url-rule-template
(You forgot to show the cgi scope dump, fyi)
Are you certain that Application.cfc/Application.cfm isn't just showinng the login page? Also look in any header files. That's the likely scenario always showing the login page.
Anyway, you can use .htaccess to do just this but it's not clear whether you want each subdirectory to its index.cfm, or if you want to redirect to the root.
If you want the redirect to the index page of the current directory (/something/ redirects to /something/index.cfm)
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^((.*)$) /$1/index.cfm [L,R=301]
If, instead, you want it to redirect to the root folder, that's a pretty simple change
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^((.*)$) /index.cfm [L,R=301]
The L in both examples stops processing of rules after that, so if you have other rules in .htaccess, that may be a concern.
The R flag (R=301) is important to cause the URL to visibly change, creating the effect you want.

.htaccess redirect files with certain paths to another directory

This might seem a bit basic and something that's been asked quite a lot around here, but I have a small .htaccess problem (mod_rewrite).
I'm working on a MVC framework for PHP (like everybody else...) and all traffic goes through index.php which then routes to the required controller and method. All that goes well. The structure is roughly something like this:
application
controllers
models
views
cache
framework
public
assets
img
css
js
index.php
.htaccess
For an URL like myapp.com/css/ I need o load the CSS controller, index function. But for URLs like myapp.com/css/style.css I need to fetch the file from the public/css/directory
I'd hate writing /public/ for each file I want to include, so basically I need to redirect all traffic to /public/ if it's an actual file and keep the normal rewriting rule for all other URLs. I'm planning to use this in production and it would be much easier to let frontend developers do their stuff the way they normally do it and then just copy paste stuff into place instread of going through CSS to modify paths and such.
I came up with this:
RewriteEngine on
RewriteRule ^(img|css|js|assets)/(.*).([a-z]{3})$ public/$1/$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
But it has some obvious flaws. I don't mind having to set directories in the first regex, but checking that the path is a file the way I do seems rather unreliable. Using RewriteCond to check that it's a file didn't work for some reason and I think this method can fail for URLs like myapp.com/img/this-is-actually-an-article.aaa Of course, extensiuons can also be longer than 3 characters and I need to check that that's safe as well.
What's the best way to go about this? How do you guys ussually do it? Or is this a wrong approach from the very begining?
The -f isn't working because the requested file is /css/style.css whereas the file on disk is /public/css/style.css.
I see no problem with declaring some predefined namespaces that you can't use in your application (like img, css, js, assets).
Eventually I think you will move to a situation where plugins for your framework can no longer decide where their resources are, the framework should decide it for them and possibly even load it for them. This resolves all your current issues as no plugin code will ever need to know anything about URLs. Regardless of your rewriting strategy I think this is something to aspire to.

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.

Codeigniter Routes for filename with extension

I am using codeigniter and its routes system successfully with some lovely regexp, however I have come unstuck on what should be an easy peasy thing in the system.
I want to include a bunch of search engine related files (for Google webmaster etc.) plus the robots.txt file, all in a controller.
So, I have create the controller and updated the routes file and don't seem to be able to get it working with these files.
Here's a snip from my routes file:
$route['robots\.txt|LiveSearchSiteAuth\.xml'] = 'search_controller/files';
Within the function I use the URI helper to figure out which content to show.
Now I can't get this to match, which points to my regexp being wrong. I'm sure this is a really obvious one but its late and my caffeine tank is empty :)
You should not need to escape the full stop, CodeIgniter does most of the escaping for you.
Here is a working example I use:
$route['news/rss/all.rss'] = "news/rss";
Issue was actually in .htaccess file where I had created a rewrite exception to allow the search engine files to be accessed directly rather than routing them through codeigniter.
RewriteCond $1 !^(index\.php|google421b29fc254592e0.html|LiveSearchSiteAuth.xml|content|robots\.txt|favicon.ico)
Became
RewriteCond $1 !^(index\.php|content|favicon.ico)