Localhost htaccess无法正常工作

时间:2015-07-25 08:49:31

标签: php apache .htaccess mod-rewrite

我有以下.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 /  -project1 /  -project2 /  -coffee_shop /
  2. 我不想将.htaccess移动到root用户,因为我可能会在其他项目中使用不同的.htaccess。

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

    2. 尝试添加到httpd.conf <Directory /Applications/XAAM/htdocs/coffee_shop>AllowOverride All</Directory>,但它不起作用。

    3. 有什么建议吗?

      感谢。

1 个答案:

答案 0 :(得分:2)

对我而言,你似乎错过了&#34; html&#34;部分重写规则 - 它在网址中引用但不是规则 - 因此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