php - Htaccess 路由返回 404 not found
问题描述
最近,我尝试将 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 found and htaccess problem 404 not found but nothing works
解决方案
您可以使用以下内容:
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
文件的。
推荐阅读
- google-cloud-platform - Google Cloud Platform 计算错误率图表为 100%
- regex - 如何限制正则表达式中 2 个相邻字符串的总长度?
- solr - SOLR 精确搜索关闭词干提取
- python-3.x - 什么情况下过滤器可以拒绝在 Python 中工作?
- vue.js - 带有 v-for 的 srcset
- python - 为 MAC 版本 10.14.6 安装 python
- hotchocolate - 使用 Pure Code First Hot Chocolate GraphQL 的特定属性的日期类型
- javascript - Firestore 获取与值数组中的项目匹配的任何值
- amazon-s3 - S3 存储桶策略和 EMR - 示例
- javascript - ReactJS:为什么我的反应主应用程序组件在由键盘键而不是鼠标单击触发时会呈现多次?