【问题标题】:404 errors with .htaccess and php files.htaccess 和 php 文件的 404 错误
【发布时间】:2015-01-15 07:42:11
【问题描述】:

我在输入时不断收到 404 错误。

localhost/shirt.php?id= 后跟我创建的数组中的一个数字

RewriteEngine On
RewriteRule ^shirts/$ /shirts/shirts.php
RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1
RewriteRule ^receipt.php$ /receipt/ [R=301]
RewriteRule ^contact.php$ /contact/ [R=301]
RewriteRule ^shirts.php$ /shirts/ [R=301]

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^shirt.php$ /shirt/%1/? [R=301]

在 Chrome 中使用 MAMP,我已经多次清除缓存

【问题讨论】:

  • 啊,301 Moved Permanently 的快乐!在你真正关闭浏览器并重新打开它之前,你会很开心(即使那样仍然可能)。在开发过程中,你永远不应该使用[R=301],因为浏览器缓存会非常激进地重定向。
  • 这是你想要发生的事情:localhost/shirt.php?id= 在浏览器中请求,RewriteRule ^shirt.php$ /shirt/%1/? [R=301] 被匹配,301'ed 到 RewriteRule ^shirts/([0-9]+)/$ /shirts/shirt.php?id=$1?如果是这样,您没有规则来捕获新 URL
  • 你想做什么?您想在浏览器中显示哪些 URL,哪些应该是内部 URL?
  • @Boldewyn 刚刚关闭了 chrome 重新打开了另一个 404
  • @anubhava 我的目标是像 localhost/shirt/101 这样的东西,但在我的本地服务器上它看起来像 local/shirt.php?id= 数组中的任何东西

标签: php .htaccess mamp server


【解决方案1】:

您可以在DOCUMENT_ROOT/.htaccess 文件中使用此代码:

Options -MultiViews
RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+shirt\.php\?id=([^\s&]+) [NC]
RewriteRule ^ shirt/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^shirt/(\d+)/?$ shirt.php?q=$1 [L,QSA,NC]

确保您在测试时没有任何其他规则。

【讨论】:

  • 对我不起作用,但我摆弄它,现在它起作用了吗?只需要逐步重写所有内容,谢谢
  • 很高兴知道它成功了。如果您认为此答案对您有帮助,那么您可以通过单击我的答案左上角的勾号将答案标记为已接受。
【解决方案2】:

所以在熬夜几个小时后,我想通了

在网址末尾添加斜杠

RewriteRule ^(contact/[a-zA-Z0-9]+)$ /$1/ [R=301]

用查询字符串重写规则

RewriteRule ^contact/([a-zA-Z0-9]+)/$ /contact/index.php?status=thanks
RewriteCond %{QUERY_STRING} ^status=([a-zA-Z0-9]+)$
RewriteRule ^index.php$ /contact/%1/? [R=301]

这对我有用

【讨论】:

    猜你喜欢
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2015-08-22
    • 2018-10-11
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多