camursm
camursm

Reputation: 193

htaccess - subfolder as DocumentRoot for CSS files?

How do I use the .htaccess file to set a custom DocumentRoot for CSS files? All CSS files are within a folder named "assets", and I'd like to omit the "assets" folder when loading in the CSS files for an HTML page.

This is the current code I am using:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !assets/
RewriteRule (.*) /assets/$1 [L]

This code makes links relative to the assets folder, but it doesn't apply to CSS files for some reason, as I still need to use href="assets/styles.css" in order to load a CSS file from /assets. I'd like to simply use href="styles.css".

Upvotes: 0

Views: 1910

Answers (2)

Waruna Manjula
Waruna Manjula

Reputation: 3497

I would create only one .htaccess file to solve all your problems. Please put this into your /root folder and delete the other .htaccess files.

# This first part should be done by the webserver,
# if not than thing about to change you hoster but I put it here:
# Preventing direct access to any .ht file (.htaccess, .htpasswd, etc.)
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

Options +FollowSymlinks
Options -Indexes

# Start to Rewrite
RewriteEngine On

# For all URL starting with /css, /fonts, /img or /js
RewriteCond %{REQUEST_URI} ^/?(css|fonts|img|js)(/.*)?$ [NC]
RewriteRule ^.*$ /site/public/%1%2 [L]

# Redirect all to the Application if not done already
RewriteCond %{REQUEST_URI} !^/?site/public/index\.php [NC]

# but not if the URL starts with css, fonts, img or js
RewriteCond %{REQUEST_URI} !^/?(css|fonts|img|js)(/.*)?$ [NC]

# or if request is a real file
RewriteCond %{REQUEST_FILENAME} !-f

# or if request is a real directory but not the root directory
RewriteCond %{REQUEST_URI} ^/?$ [OR]
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite the rest to the index.php file in your public folder
RewriteRule ^.*$ /site/public/index.php [NC,L]

Upvotes: 1

camursm
camursm

Reputation: 193

It worked... I just refreshed my browser cache, and the CSS files was included properly.

Upvotes: 0

Related Questions