Martyn
Martyn

Reputation: 6383

Trying to apply mod rewrite rule for e.g. css/style.123456.css -> css/style.min.css

I'm trying to create a rewrite rule for any js/css files with a given time stamp to point to the real files but not really sure how to do it. Below is my attempt but when I try this it seems to mess up my

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(js|css)\/(.+)\.(\d+)\.(js|css)$ $1/$2.min.$4
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

By the way, my folder structure is as follows:

/public/
/public/index.php
/public/.htaccess (rules are written in this file)
/public/css/style.min.css
/public/js/all.min.js

Also, the apache vhost conf file will direct requests to the /public/ directory. This I can verify works. But when I try to view /css/style.12235543543.css (which I want to point to /css/style.min.css) it just goes through the front controller (index.php) instead of the stylesheet.

Upvotes: 0

Views: 88

Answers (1)

Croises
Croises

Reputation: 18671

Use that:

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]

    RewriteRule ^(js|css)/(.+)\.(\d+)\.(js|css)$ $1/$2.min.$4 [L]
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

Upvotes: 2

Related Questions