Tikas Mamed
Tikas Mamed

Reputation: 89

.htaccess if request is a file or directory, redirect with absolute path

With this htaccess configuration, I have to set all my .js and .css or image path with absolute path.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]

How do I have to modify this htaccess so i can put path of the image, css, or js files doesn't have to be an absolute path?

Is it possible?

Thanks before.


ADD

Say my directory structure like this:

root
-- asset
   -- images
   -- css
   -- js

Upvotes: 0

Views: 4243

Answers (1)

Jon Lin
Jon Lin

Reputation: 143916

It's probably easier to just add a relative URI base to your page's header:

<base href="/" />

But if you must use htaccess, and all of your scripts and styles are in the same place, you can trap it with a condition:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/path/to/css
RewriteRule ([^/]+\.css)$ /path/to/css/$1 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/path/to/js
RewriteRule ([^/]+\.js)$ /path/to/js/$1 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/path/to/images
RewriteRule ([^/]+\.(png|jpe?g|gif))$ /path/to/images/$1 [L,NC]

EDIT:

if the path to your js/css/images are all in the same place, you can try combining the rules:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/assets/
RewriteRule ^(css|js|images)/(.*)$ /assets/$1/$2 [L,NC]

Upvotes: 1

Related Questions