【问题标题】:Apache is rewriting to index.php, but does not attach the query stringApache 正在重写 index.php,但未附加查询字符串
【发布时间】:2011-06-23 13:05:19
【问题描述】:

我正在尝试让 .htaccess 在我的本地主机中工作(我有 Mac OS X 10.5)。我通过关注this one 之类的文章启用了.htaccess。但是,尽管现在正在进行重写,但它无法正常工作。例如,重写规则之一是:

RewriteRule ^(home|faq|about|contact)$ index.php?section=$1 [QSA,NC]

所以当我输入http://localhost/~MyUsername/MyWebsite/home 时,应该会显示http://localhost/~MyUsername/Mywebsite/index.php?section=home ...但是,会发生 404 错误,并且错误消息是“请求的 URL /Users/MyUsername/Sites/MyWebsite/index.php在此服务器上找不到。”

我的猜测是我的 Apache 配置中有问题(重写规则在我的在线主机中运行良好),但如何解决它超出了我的知识范围。有任何想法吗?提前致谢。

【问题讨论】:

  • /Users/MyUsername/Sites/MyWebsite/index.php 存在吗?
  • 我忘记包含“RewriteBase /”命令。现在我这样做了,404 错误消息显示:“在此服务器上找不到请求的 URL /index.php。”
  • 这不能被视为答案 - 但安装 MAMP 或类似的包(或只是正确配置你现在拥有的)。您现在面临的 URL 重写问题可能与您使用 ~MyUsername/... 类型的路径有关。理想情况下,要访问您的主页,您应该只输入http://localhost/(或非常相似),仅此而已(没有/~MyUsername/MyWebsite/ 部分)。这使得 URL 重写变得更加困难(尤其是当涉及到用户的主文件夹时)。
  • 简而言之——Apache 可能没有足够的访问权限来访问您主文件夹中的网站文件。尝试将网站移出/Users/ 文件夹。

标签: apache .htaccess localhost


【解决方案1】:

试试下面的 RewriteRule:

RewriteRule ^/(.*)/(home|faq|about|contact)$ /$1/index.php?section=$2 [QSA,NC]

你可以在这里测试你的重写规则 - http://martinmelin.se/rewrite-rule-tester/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2012-08-06
    • 2021-02-04
    • 1970-01-01
    • 2015-11-15
    • 2016-08-13
    • 2016-11-01
    相关资源
    最近更新 更多