首页 > 解决方案 > 重写 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 错误,我不知道这是解决方案,在此先感谢

标签: phpregex.htaccessmod-rewriteurl-rewriting

解决方案


您的原始 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>

推荐阅读