Sign in to follow this  
Colin Jeanne

[web] Dynamic URLs

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I am still confused. I know that index.php will be sent a text/html but the link tag tells the browser to interpret the URI as text/css, right?

The effect of the link tag on the interpretation of the file is what's confusing me [smile].

Share this post


Link to post
Share on other sites
Quote:
I am still confused. I know that index.php will be sent a text/html but the link tag tells the browser to interpret the URI as text/css, right?
Perhaps, but the browser will be confused when it requests the linked stylesheet and gets an HTML file. It's the Content-type header that matters.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this