【问题标题】:Htaccess routing returns 404 not foundHtaccess 路由返回 404 not found
【发布时间】:2020-12-04 03:01:24
【问题描述】:

最近,我尝试将 htaccess 用于我的语义清理器 url。这是我的代码

Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteRule ^product/([0-9]+)$ home.php?id=$1 [NC]

当我导航到 http://localhost/home.php?id=1 时,它会返回正确的内容,但是当我转到 http://localhost/products/1 时,它会返回 404 not found 错误。

我尝试了这个问题:htaccess rewrite returns 404 not foundhtaccess problem 404 not found 但没有任何效果

【问题讨论】:

  • 尝试在第一行禁用多视图 Options -Multiviews+ 更改为 -
  • 仍然遇到同样的问题

标签: php apache .htaccess mod-rewrite


【解决方案1】:

您可以使用以下内容:

RewriteEngine on
Options -Multiviews
#rewrite /filename to /filename.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ $1.php [L]
# /product/123 to home.php?id=123
RewriteRule ^product/([0-9]+)$ home.php?id=$1 [NC]

当请求的 URI 为 /product/1 时,您的规则返回 404,因为您的第一条规则将其重写到未知位置 /product/1.php 。我在第一条规则中添加了一个条件,以便它检查请求是否实际上是针对 .php 文件的。

【讨论】:

  • 终于成功了!谢谢!但是你能详细说明我哪里做错了吗?
猜你喜欢
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多