首页 > 解决方案 > .htaccess 检测由“-”分隔的数字

问题描述

我在网上找不到任何有用的资源,所以我只是在这里问
我有我的 .htaccess 文件,其中包含以下代码行:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^KOP/shop/(\d+)*$ KOP/shop/details.php?id=$1 [L]
RewriteRule ^KOP/order/(\d+)*$ KOP/order.php?id=$1 [L]

当我输入
localhost/KOP/order/1612-8077-68 时
,出现找不到页面错误

但是当我输入
localhost/KOP/order/1612807768
重写工作,并从 $_GET 中写出数字

我想知道我应该在我的代码中改变什么,让它用数字之间的破折号重写。我的 .htaccess 文件中有一行告诉 apache 只重定向数字吗?

标签: apache.htaccessmod-rewriteurl-rewritingfriendly-url

解决方案


根据您显示的示例,您能否尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^KOP/shop/(\d{4})-(\d{4})-(\d{2})$ KOP/shop/details.php?id=$1$2$3 [L]
RewriteRule ^KOP/order/(\d{4})-(\d{4})-(\d{2})$ KOP/order.php?id=$1-$2-$3 [L]  
// Remove the dashes to get only the numbers ↑

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^KOP/shop/(\d+)*$ KOP/shop/details.php?id=$1 [L]
RewriteRule ^KOP/order/(\d+)*$ KOP/order.php?id=$1 [L]

推荐阅读