【问题标题】:Why this htaccess file is not working in wamp server为什么这个 htaccess 文件在 wamp 服务器中不起作用
【发布时间】:2014-09-02 15:13:00
【问题描述】:

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

【问题讨论】:

    标签: php .htaccess wamp wampserver


    【解决方案1】:

    这是因为/news/2 创建了一个虚拟目录。
    由于您为 html 链接使用 relative 路径,因此它不起作用。

    假设你有一个像这样的 css 链接
    &lt;link rel="stylesheet" type="text/css" href="style.css"&gt;
    由于您的 URI 是 /news/2,它将在 /news/style.css 中搜索,这不是您想要的。

    改为使用绝对路径。
    例如:&lt;link rel="stylesheet" type="text/css" href="/style.css"&gt;(带有前导斜杠,如果不在根文件夹中,可能还有另一个基数)。

    此外,在您的 htaccess 中放一个 RewriteBase /(或在 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 前缀) &lt;link rel="stylesheet" type="text/css" href="/project_news/css/style.css"&gt;
    • 或者,如果您不想更改所有链接,也可以在&lt;head&gt; html 标记之后添加&lt;base href="/project_news/"&gt;(在所有相关的html 页面中)

    然后,用这个替换您当前的 htaccess 代码

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteBase /project_news/
    
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ index.php [L]
    </IfModule>
    

    【讨论】:

    • 嗨,兄弟,我正在开发 wamp 本地服务器,当我添加 RewriteRule ^ /index.php [L] 时,它会将我重定向到 localserver 主页,我的 css 就像
    • 你应该把它写在你的问题中,否则它不完全一样。您能告诉我您的项目在哪个文件夹中吗? (您使用哪个 localhost-url 来访问它?) css 文件夹呢,它也在项目文件夹中吗?
    • 我的项目在 project_news 是的 css 在同一个文件夹 localhost/project_news
    • 好的,你可以看到我编辑的答案,现在应该可以按预期工作
    猜你喜欢
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    相关资源
    最近更新 更多