我想使用.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¤tpage=$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¤tpage=$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。