mod_rewrite apache2.4 规则不起作用

mod_rewrite apache2.4 规则不起作用

我正在尝试使用 Apache 2.4 配置 .htaccess 以实现非常简单的 URL 重定向

来自:/order/step1.php?service=151
至:/order/step-1/151

我的.htaccess 是:

RewriteEngine On
RewriteRule ^/?/([^/d]+)/?$ /order/step1.php?service=$1 [L,QSA]

我确信我遗漏了一些古怪的东西,有人知道我做错了什么吗?

答案1

您的模式与给定的“发件人”文本不匹配。

您目前正在匹配:

^ beginning of the text
/? an optional slash
/ a slash
([^/d]+) a sequence of 1 or more characters not matching a slash and the letter "d" (and capture this text)
/? an optional slash
$ end of the text

您需要匹配:

^ beginning of the text
/order/ this literal text
([^0-9-]+) match all characters except digits and a dash, and capture this
- a dash
([0-9]+) match all digits and capture this
\.php\?service= match ".php?service="
([0-9]+) match all digits and capture this

然后您可以使用这些捕获的字符串来构建替换:

/order/$1-$2/$3

因此,RewriteRule 的最后一行是:

RewriteRule ^/order/([^0-9-]+)-([0-9]+)\.php\?service=([0-9]+)$ /order/$1-$2/$3 [L,QSA]

相关内容