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

Redirect css|js|img|fonts folder using .htaccess

我在一些网站所在的共享主机上。

我必须通过将 ZendFramework2 添加到根目录(目录名称 = 站点)中来移动此服务器上的 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

.htacces 在 /site 文件夹

RewriteEngine On

RewriteRule ^\.htaccess$ - [F]

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

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

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

RewriteRule ^site/public/.*$ /site/public/index.php [NC,L]

.htacces 在 /site/public 文件夹

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 标签:

<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

非常欢迎任何帮助!

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