Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I'm rather new to the whole .htaccess thing and I'm using the following right now to use 'pretty url's':

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
</IfModule>

Now i found my website a bit slow and decided to start gzipping my CSS files thru a php script I found somewhere on the web (the website). For this to work I need to rewrite the url to open the correct php file. That would look something like this:

RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]

But I only want the first to happen when the second doesn't and vice versa. In other words i'd like something like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    if request doesn't contain .css do
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
    else do
        RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>

Can anyone help me with the proper code or a place where i can find a way to use some kind of conditional statement in htaccess files?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

I will go onto something like that.

First you define a new condition (RewriteCond) then you apply this condition using a rule (RewriteRule).

Thanks to the flag L at the end of the RewriteRule, this stop the rewriting process immediately and don't apply any more rules. So when the request is a .css file, the first rule will be apply and not the second one. For all others request, it won't match the first condition, then go to the second one.

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} ^(.*)\.css$
    RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
</IfModule>
share|improve this answer
    
Thanks, works perfectly! –  dragon112 Sep 12 '12 at 9:33
add comment

You should not do this, use Apache deflate module to Gzip you files. This is what I use in .htaccess :

<IfModule deflate_module>
        AddOutputFilterByType DEFLATE text/php text/html text/txt text/javascript text/css application/javascript application/x-javascript
</IfModule>

Check out the documentation to have more info on the mime type and the syntax : http://httpd.apache.org/docs/2.2/mod/mod_deflate.html

share|improve this answer
    
Why should you not compress the files the way the OP has done? –  w3d Sep 12 '12 at 11:40
    
Putting an additional RewriteRule in the htaccess and using a PHP script to compress in gzip, when an Apache module exists. Just simpler, less potential bugs, more performance... Do I have to say more? –  Baptiste Placé Sep 12 '12 at 13:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.