php - 为什么这个 htaccess 文件在 wamp 服务器中不起作用

标签 php .htaccess wamp wampserver

我有这个 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/

相关文章:

php - 如果带有条件前缀 [+ 和后缀 +],则获取字符串的一部分

php - 当我使用 .htaccess 重定向所有内容时,Bootstrap css 不工作

php - 防止 IP 在服务器上多次下载文件

apache - WAMP Apache 未启动

php - WAMP 无法执行菜单项(内部错误)

php - 如何在 PDO::FETCH_FUNC 中使用对象方法

php - 如何获取帖子列表并检查用户是否已经将它们设为 "Liked"? (检查两个模型之间的现有关系)

javascript - 我可以用数字动态更改图像然后将其打印为 PDF 吗

php - htaccess 重写规则请求 URI 问题

wordpress - 在 80 以外的端口上运行 Wordpress