Question

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">
Était-ce utile?

La solution

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]

Autres conseils

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.

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]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top