RewriteRule for static files in subfolder

拜拜、爱过 提交于 2019-12-24 06:44:40

问题


I have following folder structure for my website:

index.php
.htaccess
public_html/css/* (css files)
public_html/images/* (image files)
public_html/js/* (javascript files)

All the static files in the subfolder public_html are linked in my page files without public_html: e.g. /images/logo/logo.png

I setup following .htaccess file, which is working fine on my local apache and on my webspace, but on the customers webspace, it is not working:

RewriteEngine On
RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/public_html/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /public_html/$1 [L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond ${REQUEST_URI} ^/public_html/
RewriteRule .* - [L]
RewriteRule ^ index.php [QSA,L]

回答1:


This line

RewriteCond %{DOCUMENT_ROOT}/public_html/%{REQUEST_URI} -f

must be rewritten to (2 possibilities)

RewriteCond %{DOCUMENT_ROOT}/public_html%{REQUEST_URI} -f
# or
RewriteCond %{DOCUMENT_ROOT}/public_html/$1 -f

Also, you don't need leading slash / before public_html in RewriteRule since you already defined it in RewriteBase.

Then, this line

RewriteCond ${REQUEST_URI} ^/public_html/

must be rewritten to

RewriteCond %{REQUEST_URI} ^/public_html/

typo error (replace $ by %).

Conclusion

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^/public_html/ [NC]
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}/public_html/$1 -f
RewriteRule ^(.*)$ public_html/$1 [L]

RewriteRule ^ index.php [L,QSA]


来源:https://stackoverflow.com/questions/25686583/rewriterule-for-static-files-in-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!