Custom Rewrite

New to mod_rewrite? This is a good place to start.

Custom Rewrite

Postby dogtag » Sat Aug 22, 2009 12:40 am

Hello,
I wrote this in my .htaccess file:
Code: Select all
RewriteRule ^(.[^\.]+).js$  $1.gz.js [L,NC,QSA]
RewriteRule ^(.[^\.]+).css$  $1.gz.css [L,NC,QSA]


And it works perfect.

But I want to disable this for all of my subdomains or just my "test" folder.

How can I do this?

I did many things but It is not working at all!
dogtag
 
Posts: 7
Joined: Sat Aug 22, 2009 12:25 am

Postby richardk » Mon Aug 24, 2009 2:08 pm

Code: Select all
RewriteRule ^(.+)\.(css|js)$  /$1.gz.$2 [NC,QSA,L]


But I want to disable this for all of my subdomains

Code: Select all
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]


or just my "test" folder.

Code: Select all
RewriteCond %{REQUEST_URI} !^/test(/.*)?$ [NC]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dogtag » Tue Aug 25, 2009 2:02 am

Thanks a lot.

Here is my .htaccess CODE:
Code: Select all
<IfModule mod_headers.c>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_URI} !^/test(/.*)?$ [NC]
RewriteRule ^([^\.]+)\.(css|js)$  $1.gz.$2 [NC,QSA,L]
</IfModule>


This is good, but it needs this line too:
Code: Select all
Header set Content-Encoding "gzip"


I add it in a separated .htaccess file in each subfolder.

Is there any way to add it to the main .htaccess? (I mean the CODE above)

thanks a lot.
waiting...
dogtag
 
Posts: 7
Joined: Sat Aug 22, 2009 12:25 am

Postby richardk » Sat Aug 29, 2009 2:06 pm

What happens if you put it in the main .htaccess file?
Have you got other rules in the sub directories?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dogtag » Sun Aug 30, 2009 12:28 am

richardk wrote:What happens if you put it in the main .htaccess file?
Have you got other rules in the sub directories?


I don't know how to put it in main .htaccess.
I want this to gzip only CSS & JS files.
but when It is in main .htaccess (not css/js files' folder), it GZIPs all file (PHP, JPG, ...).

Is there anyway to assign it just to CSS/JS files?

thanks a lot
dogtag
 
Posts: 7
Joined: Sat Aug 22, 2009 12:25 am

Postby richardk » Sun Aug 30, 2009 11:10 am

Do you mean this line
Code: Select all
Header set Content-Encoding "gzip"

?

You can use <FilesMatch>
Code: Select all
<FilesMatch ".\.(css|js)$">
  Header set Content-Encoding "gzip"
</FilesMatch>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dogtag » Sun Aug 30, 2009 11:40 am

thanks a lot.
It works perfect! :wink: :P
dogtag
 
Posts: 7
Joined: Sat Aug 22, 2009 12:25 am

Postby dogtag » Mon Aug 31, 2009 5:01 am

another problem.
let me tell what I want to do:

If requested file is a css/js file THEN
If browser supports GZIP compression THEN
If FILENAME.gz.(js/css) exists THEN
Rewrite this file with the .gz extension

I think this code is good for all conditions except the BOLD one:
Code: Select all
<FilesMatch "\.(css|js)$">
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_URI} !^/test(/.*)?$ [NC]
RewriteRule ^([^\.]+)\.(css|js)$  $1.gz.$2 [NC,QSA,L]
Header set Content-Encoding "gzip"
</FilesMatch>


Can someone please fix it?

thanks a lot
dogtag
 
Posts: 7
Joined: Sat Aug 22, 2009 12:25 am

Postby richardk » Mon Aug 31, 2009 8:13 am

Try
Code: Select all
<FilesMatch "\.(css|js)$">
  Header set Content-Encoding "gzip"

  RewriteCond %{HTTP:Accept-Encoding} gzip
  # Check if the file exists.
  RewriteCond %{DOCUMENT_ROOT}/$1.gz.$2 -f
  RewriteRule ^([^\.]+)\.(css|js)$ /$1.gz.$2 [NC,QSA,L]
</FilesMatch>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dogtag » Tue Sep 01, 2009 12:06 am

richardk wrote:Try
Code: Select all
<FilesMatch "\.(css|js)$">
  Header set Content-Encoding "gzip"

  RewriteCond %{HTTP:Accept-Encoding} gzip
  # Check if the file exists.
  RewriteCond %{DOCUMENT_ROOT}/$1.gz.$2 -f
  RewriteRule ^([^\.]+)\.(css|js)$ /$1.gz.$2 [NC,QSA,L]
</FilesMatch>


It is not tested, but I think it won't work!

it will GZ all css/js files.
I want to send the GZIP header only if the FILE.gz.(js/css) exists.

---

Let me ask in another way:
Is there any way to GZIP js/css files automatically by htaccess?
dogtag
 
Posts: 7
Joined: Sat Aug 22, 2009 12:25 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 31 guests

cron