重写规则不起作用并导致CSS错误


ReWrite Rule Not Working & Causing CSS Errors

我想使用.htaccess的RewriteRule,但我遇到了一些问题。 我使用以下方法测试了该规则:

RewriteRule ^newpage articles.php [NC,L]

当我访问blog.local.com/newpage时,我会看到articles.php,所以我知道我的htaccess工作正常。

但是,当我使用:

DirectoryIndex articles.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^articles/([0-9a-zA-Z_-]+)/([0-9]+) articles.php?cat=$1&currentpage=$2 [NC,L]

并访问blog.local.com/articles/all/2,我看到了第一篇文章页面,但它显示没有任何CSS样式。 谁能帮我解决这个问题?

您需要

关闭MultiViews选项。

Options -MultiViews
DirectoryIndex articles.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^articles/(['w-]+)/([0-9]+)/?$ articles.php?cat=$1&currentpage=$2 [NC,L,QSA]

选项 MultiViews 由在mod_rewrite之前运行的Apache's content negotiation module使用,并使 Apache 服务器匹配文件的扩展名。所以/file可以在 URL 中,但它会/file.php服务。

同样为了解决css/js问题,您可以在页面HTML的<head>部分下方添加以下内容:

<base href="/" />

以便从该基本 URL 而不是从当前页面的 URL 解析每个相对 URL