使用.htaccess重定向css | js | img | fonts文件夹

时间:2017-09-28 15:34:54

标签: javascript css .htaccess zend-framework2

我正在共享托管,其中有一些网站。

我必须通过在根目录(dir name = site)的目录中添加ZendFramework2来在此服务器上移动它。控制器和操作正常,但我无法加载任何CSS,图像或JavaScript。

以下是我的目录:

/root/website1 (example.com/website1)
/root/website2 (example.com/website2)
/root/site     (example.com/site)
/root/site/application
/root/site/data
/root/site/library
/root/site/public
/root/site/public/css
/root/site/public/fonts
/root/site/public/img
/root/site/public/js
/root/site/public/.htaccess
/root/site/.htaccess
位于/ site文件夹中的

.htacces

RewriteEngine On

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /site/public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/site/public/.*$
RewriteRule ^(.*)$ /site/public/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^site/public/.*$ /site/public/index.php [NC,L]
/ site / public文件夹中的

.htacces

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /site/index.php [NC,L]

css tag:

<link href="/css/global.css" rel="stylesheet" type="text/css" media="screen" />

我可以重定向/css/global.css以加载/site/public/css/global.css吗?

当然,我可以改变......

<link href="/site/css/global.css" rel="stylesheet" type="text/css" media="screen" />

...但我不想改变href

非常欢迎任何帮助!

1 个答案:

答案 0 :(得分:2)

我只会创建一个.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]