如果找不到文件,mod_rewrite加载css和js


mod_rewrite load css and js if file not found

我的httpd.conf中有这些行,它们将所有url作为一个参数('name'),当我键入一个不存在的名称时,index.php将正确加载,并显示404消息。但问题是JavaScript, CSS and other assets are not loaded.如何加载这些文件,即使目录/文件不存在?我正在使用relative paths。谢谢;)

<Directory  "c:/Apache24/htdocs/public">
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.+)$ index.php?name=$1 [QSA,L]
    </IfModule>
</Directory>

一般来说,FallbackResource指令是一种更干净、更高效的方式,可以实现您在这里要做的事情:

http://httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource

不用mod_rewrite的东西,只需写以下内容:

FallbackResource index.php

在PHP脚本中,您可以将PATH_INFO环境变量转换为用于捕获name参数的任何变量。