Page 1 of 1

RewriteRule to exclude CSS

Posted: Tue Jun 02, 2009 2:52 pm
by jayshields
I've got a simple .htaccess file which contains:

Code: Select all

RewriteEngine On
RewriteRule !^(css|images|files)/ index.php [NC,L]
 
ErrorDocument 404 /
This basically just sends everything to my front controller.

I'm using my HTML template in quite a dodgey (probably poor) fashion, as below.

Code: Select all

//Grab the HTML template
ob_start();
include 'frontend/layout.php';
$template = ob_get_contents();
ob_end_clean();
 
$templateBits = explode('***CONTENT***', $template);
$header = $templateBits[0];
$footer = $templateBits[1];
 
//Output the page header
echo $header;
 
// ...
 
//Output the page footer
echo $footer;
 
layout.php is basically just a HTML template with some bits of PHP in it. I'm trying to include my stylesheet in it with this line.

Code: Select all

<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
style.css is in the same directory as layout.php (frontend). I've tried loads of paths (even absolute paths) to try get to my CSS file but it's not working. I guess it's something to do with the .htaccess file. Does anyone know how I can correct it?

Thanks.

Re: RewriteRule to exclude CSS

Posted: Tue Jun 02, 2009 2:57 pm
by mikemike
Try something like:

Code: Select all

RewriteEngine on
RewriteCond $1 !^(index\.php|style\.css|images|files|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
 
That should send everything to index.php other than calls to 'index.php' itself, style.css, images dir, files dir, css dir and your robots.txt

Re: RewriteRule to exclude CSS

Posted: Tue Jun 02, 2009 2:59 pm
by mikemike
...and in exchange for that excellent bit of knowledge can you do me a cracking deal on a Porsche Cayman please? :D

Re: RewriteRule to exclude CSS

Posted: Tue Jun 02, 2009 3:50 pm
by jayshields
Sweet, it works. Cheers for that.

I don't think we/they have any Porsche Caymans for sale at the moment!

Re: RewriteRule to exclude CSS

Posted: Tue Jun 02, 2009 4:03 pm
by mikemike
Poor show!

Re: RewriteRule to exclude CSS

Posted: Tue Jun 02, 2009 4:49 pm
by jayshields
On closer inspection, it doesn't work. It's because I'm not using my Apache document root. If I take the slash out from before index.php in the RewriteRule then it redirects fine, but the CSS still doesn't work.

Code: Select all

RewriteEngine on
RewriteCond $1 !^(index\.php|style\.css|images|files|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
 
ErrorDocument 404 /
I had the CSS working with the redirects not working (when specifying a page) a bit ago but can't get it back.

Edit: Sorted it. Added frontend to the list of things in the RewriteCond.