【问题标题】:mod_rewrite not working correctly - accessing static resourcesmod_rewrite 无法正常工作 - 访问静态资源
【发布时间】:2011-12-30 22:22:04
【问题描述】:

现在我的网站根目录中有几个文件夹(img、css、js 和 ico)。但是,我想将它们移动到一个名为 public_html 的新目录中。例如,图像文件夹不是位于 /img 中,而是位于 /public_html/img 中。但是我在这样做时遇到了问题,我怀疑这是我的 .htaccess 文件的问题。

Options -Indexes
Options +FollowSymLinks
RewriteEngine On

ErrorDocument 404 static/404.html
ErrorDocument 403 static/403.html


RewriteCond %{REQUEST_FILENAME} !-f

# ---------- Custom Routes ----------------



# -----------------------------------------

RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2
RewriteRule ^(.*)$  index.php?r=$1  [L,QSA]

我可以很好地访问 /public_html/css/style.css,但是当我添加第一个 RewriteRule 行并尝试访问 /css/style.css 时,它不起作用。

谁能弄清楚为什么?谢谢。

【问题讨论】:

    标签: php mod-rewrite


    【解决方案1】:

    [L] 标志添加到第一条规则。否则,它会通过第二条规则并尝试将其在r= 中传递给index.php。通过public_html 访问它是因为RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2 [L]
    
    # Update: Try using the RewriteCond before this line 
    # as well as where you have it earlier
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$  index.php?r=$1  [L,QSA]
    

    【讨论】:

    • 似乎 [L] 标志不起作用,因为当我注释掉第二行时,它工作得很好。
    • @RLC 尝试在我刚刚在上面编辑的最后一行之前再次添加RewriteCond
    最近更新 更多