将URL参数重新路由到路径并保持HTML引用正常工作(htaccess)


Reroute URL parameters to path and keep HTML references working (htaccess)

我有一个URL http://example.com/?r=FOO&c=BAR,我想通过http://example.com/FOO/BAR访问它。这两个参数都是可选的。我用这个htaccess完成了这个工作:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index'.php [NC]
RewriteRule ^([^/]+)(.*)?$ index.php?r=$1&c=$2   [L]

然而,现在我意识到在我的index.php中引用的文件将不会加载。例如,我有一个http://example.com/css/app.css。在当前重写URL的情况下,浏览器尝试获取http://example.com/FOO/css/app.css并接收实际的index.php

我尝试添加<base href="/"><base href="http://example.com/">

我被困在这里了。有人知道怎么解决这个问题吗?提前谢谢。

您需要通过在RewriteBase:下添加此条件来忽略文件

RewriteCond %{REQUEST_FILENAME} !-f