nurakantech
nurakantech

Reputation: 502

load css via .htaccess

I have this very simple .htaccess rule for my site

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?$1 [QSA,L]

I routes perfectly but it is not good enough to load css and javascript files located in very different directory.

For e.g. My php files are located like this

 DOCUMENT ROOT/*.php

and My CSS files are located somewhere like this

DOCUMENT ROOT/templates/example/ui/css/xyz.css

I want the .htaccess rule such that if i write

<link rel="stylesheet" href="/css/xyz.css"/>

it loads the css file from

 http://example.com/templates/example/ui/css/xyz.css

I dont know if its possible or not. If not then please suggest any alternative ways.

Upvotes: 1

Views: 102

Answers (2)

anubhava
anubhava

Reputation: 785406

You can have an additional rule for css loading:

RewriteEngine On
RewriteBase /example/

RewriteRule ^(css/.+)$ /templates/example/ui/$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?$1 [QSA,L]

Upvotes: 2

Sumurai8
Sumurai8

Reputation: 20737

Since your current rule is more or less a catch-all rule, you want to add the following rules below RewriteEngine on, but above the catch-all rule:

RewriteCond %{REQUEST_URI} !^/templates/
RewriteRule ^(.*)\.css$ /templates/example/ui/$1.css [L]

This rule checks if the url doesn't already start with /templates. Then it rewrites all urls ending with .css to the given url. The condition is to prevent an infinite loop.

Upvotes: 1

Related Questions