RewriteRule用于子文件夹中的静态文件

时间:2014-09-05 13:03:03

标签: apache .htaccess mod-rewrite

我的网站有以下文件夹结构:

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

子文件夹public_html中的所有静态文件都链接在我的页面文件中,没有public_html:例如/images/logo/logo.png

我设置了以下.htaccess文件,该文件在我的本地apache和我的网站空间上工作正常,但在客户网站空间上,它无法正常工作:

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 个答案:

答案 0 :(得分:2)

这一行

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

必须重写为(2种可能性)

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

此外,由于您已在/中对其进行了定义,因此public_html中的RewriteRule之前不需要前导斜杠RewriteBase

然后,这一行

RewriteCond ${REQUEST_URI} ^/public_html/

必须重写为

RewriteCond %{REQUEST_URI} ^/public_html/

拼写错误(将$替换为%)。

结论

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]
相关问题