我有这个 htaccess 文件,当 url 类型为 www.mywebsite.com/news 时,它会重定向到 index.php 但是当 url 类型为 www.mywebsite.com/news/2 时,它显示 index.html 但不应用 css 我在 wamp 服务器
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
最佳答案
那是因为/news/2
创建一个虚拟目录。
由于您的 html 链接使用相对路径,因此它不起作用。
假设您有一个像这样的 css 链接
<link rel="stylesheet" type="text/css" href="style.css">
由于您的 URI 是 /news/2
它将在 /news/style.css
中搜索这不是你想要的。
相反,请使用绝对路径。
例如:<link rel="stylesheet" type="text/css" href="/style.css">
(带有前导斜杠,如果不在根文件夹中,则可能是另一个基数)。
另外,输入 RewriteBase /
也没什么坏处。在你的 htaccess 中(或 index.php
之前的前导斜杠)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
</IfModule>
编辑:为您的项目 /project_news/
在本地主机上
- 您必须以这种方式更改所有 html 链接(添加
/project_news
前缀)<link rel="stylesheet" type="text/css" href="/project_news/css/style.css">
- 或者,如果您不想更改所有链接,也可以添加
<base href="/project_news/">
就在<head>
之后html 标签(在所有相关的 html 页面中)
然后,将您当前的 htaccess 代码替换为此代码
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /project_news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
关于php - 为什么这个 htaccess 文件在 wamp 服务器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25626768/