问题
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