htaccess - 作为CSS文件的DocumentRoot的子文件夹?

时间:2018-01-10 15:57:43

标签: apache

如何使用.htaccess文件为CSS文件设置自定义DocumentRoot?所有CSS文件都在名为“assets”的文件夹中,我想在加载HTML页面的CSS文件时省略“assets”文件夹。 这是我正在使用的当前代码: 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] 此代码生成相对于assets文件夹的链接,但由于某些原因它不适用于CSS文件,因为我仍然需要使用href =“assets / styles.css”来从/ assets加载CSS文件。我想简单地使用href =“styles.css”。

2 个答案:

答案 0 :(得分:0)

它有效......我只刷新了我的浏览器缓存,并正确包含了CSS文件。

答案 1 :(得分:0)

我只会创建一个.htaccess文件来解决您的所有问题。请将其放入您的/ root文件夹,然后删除其他.htaccess文件。

# 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]