php - 重写 URL 不获取值和错误 404
问题描述
我的 Htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([0-9]+)&title=([^\s&]+)\s [NC]
RewriteRule ^ %2-%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+?)-([0-9]+)$ index.php?id=$2&title=$1 [L]
</IfModule>
我在 php 网址中有这样的:
index.php?id=34234&title=Hello_World&name=Mike
在链接中我放了这个:
<a href="index.php?id=34234&title=Hello_World&name=Mike">TEST</a>
我试着得到这个网址
http://www.test.com/34234/hello_world/mike
但是所有时间都将错误作为 404 错误,我不知道这是解决方案,在此先感谢
解决方案
您的原始 URL 有 3 个路径段,而您的规则只接受两个。你需要修复你的正则表达式模式
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([0-9]+)&title=([^\s&]+)&name=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^/]+)/(.+)/?$ index.php.php?id=$1&title=$2&name=$3 [L]
</IfModule>
推荐阅读
- android - 使用不包含 MediaQuery 的上下文调用 MediaQuery.of()。错误
- angular - kolkov/ngx-gallery 不显示图像
- flutter - 使用字符串访问主题中的 get 方法
- excel - 编写一个公式以添加 15 分钟直到到达某个时间,然后在到达该时间后打印空白?
- google-apps-script - 如何将段落标题传递给html并返回
- cloudflare - 什么是 cloudflare KV preview_ids 以及如何获得?
- android - Kotlin - 从 MainActivity 调用片段方法
- java - 恒定时间/空间复杂度概念的混淆
- amazon-web-services - AWS CLI 获取 VPC 的私有子网
- pdf - imagemagick 从多个图像中制作固定大小的 pdf