RewriteRule to exclude CSS

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

RewriteRule to exclude CSS

Post 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.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: RewriteRule to exclude CSS

Post 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
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: RewriteRule to exclude CSS

Post by mikemike »

...and in exchange for that excellent bit of knowledge can you do me a cracking deal on a Porsche Cayman please? :D
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: RewriteRule to exclude CSS

Post by jayshields »

Sweet, it works. Cheers for that.

I don't think we/they have any Porsche Caymans for sale at the moment!
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: RewriteRule to exclude CSS

Post by mikemike »

Poor show!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: RewriteRule to exclude CSS

Post 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.
Post Reply