[web] Dynamic URLs

Started by
12 comments, last by igni ferroque 18 years, 10 months ago
For a CMS I'm creating I'm implementing dynamic URLs like in this A List Apart article. Everything works perfectly but there is just something I'd like to better understand. My .htaccess file looks like this

RewriteEngine on
RewriteRule !\.(gif|jpg|jpeg|png|css)$ index.php
And the part of my index.php file which handles the dynamic URLs looks like this

// Does this file exist?
if (IsURIDynamic() == false) {
   // The file exists, simply include it
   include($_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI']);
} else {
   // The file does not exist, the content might be dynamic
   // Output template
   require_once('template.php');
}

So if the file is a real file (that is, IsURIDynamic() returns false) it is simply included. And finally, in template.php is the following line

<link rel="stylesheet" href="organs/norm.css" type="text/css" title="Blue" />
Where organs/norm.css is the real path to the actual file. The part I dont get is that if I remove 'css' from the rewrite rule in the .htaccess file then the stylesheet is not applied to template.php. I can still view the stylesheet if I navigate to its exact location and my Apache logs tell me that it is downloaded correctly when I view template.php but it just seems to not be applied. Why is the stylesheet not applied when I include() it?
Advertisement
Well the article says that the line

RewriteRule !\.(gif|jpg|png|css)$ /your_web_root/index.php

redirects all requests to a file index.php EXCEPT for requests for image files or CSS files. In other words: Removing the css part from the rewriterule redirects stylesheet requests to index.php, basically resulting in

<link rel="stylesheet" href="/your_web_root/index.php" type="text/css" title="Blue" />

which is of course not a valid stylesheet.
Yes, and then when the browser reads from that address the content of the real file is returned by include(). Only the data from the CSS file is returned so I'm not sure why it's not a valid stylesheet.
The output from any request to your index.php is sent with the default text/html MIME type. CSS stylesheets are required to be sent as text/css.
Free Mac Mini (I know, I'm a tool)
What about the type attribute on the link tag?
The browser wouldn't know how to parse HTML as a stylesheet, so you'd run into the same problem.

Does your CSS file contain PHP code?
Free Mac Mini (I know, I'm a tool)
Not at all. This is what's in the CSS file.
Then it seems like there's no reason to serve it via PHP. Just let Apache handle it.
Free Mac Mini (I know, I'm a tool)
It works fine if using .htaccess I make sure that CSS files arent redirected through my index.php I was just curious about why it didnt work when I let CSS files be redirected through index.php.
In case you were still confused, Igni's answer about it being sent as text/html is the issue.

This topic is closed to new replies.

Advertisement