【问题标题】:Localhost htaccess not working本地主机 htaccess 不工作
【发布时间】:2015-10-15 23:07:34
【问题描述】:

我有以下不工作的 .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /coffee_shop/

# For sales:
RewriteRule ^shop/sales/?$ sales.php

# For the primary categories:
RewriteRule ^shop/(coffee|goodies)/?$ /shop.php?type=$1

# For specific products:
RewriteRule ^browse/(coffee|goodies)/([A-Za-z\+\-]+)/([0-9]+)/?$ browse.php??type=$1&category=$2&id=$3

# For shop:
RewriteRule ^shop/([A-Za-z\+]+)/?$ shop.php?type=$1

</IfModule>

当我访问 http://localhost/coffee_shop/html/shop/goodies 时应该被翻译成 http://localhost/coffee_shop/html/shop?type=goodies 但事实并非如此。我收到 404 Object Not Found 错误,因此正在读取文件(我在其中放入了一些垃圾代码并导致 500 错误)。

  1. 文件夹结构为:
    • htdocs/ -项目1/ -项目2/ -咖啡店/

我不想将 .htaccess 移至根目录,因为我可能会在其他项目中使用不同的 .htaccess。

  1. 是,AllowOverride 在 httpd.conf 中设置为 All

  2. 尝试添加到 httpd.conf &lt;Directory /Applications/XAAM/htdocs/coffee_shop&gt;AllowOverride All&lt;/Directory&gt;,但没有成功。

有什么建议吗?

谢谢。

【问题讨论】:

  • 你在htaccess规则之后列出的url在路径中包含html,但重写规则中没有引用这些url

标签: php apache .htaccess mod-rewrite


【解决方案1】:

在我看来,您似乎缺少重写规则中的“html”部分 - 它在 url 中被引用但不是规则 - 因此是 404

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /coffee_shop/

    # For sales:
    RewriteRule ^html/shop/sales/?$ sales.php

    # For the primary categories:
    RewriteRule ^html/shop/(coffee|goodies)(/?)$ shop.php?type=$1

    # For specific products:
    RewriteRule ^html/browse/(coffee|goodies)/([0-9]+)(/?)$ browse.php?type=$1&id=$2

    # For shop:
    RewriteRule ^html/shop/([A-Za-z\+]+)(/?)$ shop.php?type=$1

</IfModule>


http://localhost/coffee_shop/html/shop?type=goodies can be accessed via  http://localhost/coffee_shop/html/shop/goodies

【讨论】:

  • 我添加了您在回答中提到的html,但它不起作用。是的,我确实重新启动了 apache ;)
  • 我刚刚在开发服务器上建立了一个名为 coffee_shop 的新目录,并在此处创建了三个页面。我发布的规则有效,但我现在注意到您的原始规则之一存在错误 - 即指向 browse.php 的规则 - 它应该类似于 browse.php?type=$1&id=$2 有重写规则中有 2 个占位符,最后未正确引用变量 - 并且缺少一个
  • 浏览部分是我的错误,我直接从终端复制了屏幕上的任何内容,我忘记将光标向右移动以复制浏览行中多余的部分。不过,我不知道我做错了什么,我做了你建议的更改,但我不断收到 400 错误
猜你喜欢
  • 2014-07-25
  • 1970-01-01
  • 2018-06-27
  • 2017-11-06
  • 2015-10-03
  • 2013-05-31
  • 2017-07-18
  • 2015-11-30
相关资源
最近更新 更多