您必须在 RewriteCond 中捕获查询字符串并在 RewriteRule 中使用它
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)$
RewriteRule ^/?article\.php$ /%1/%2.html? [R,L]
这会将客户端重定向到请求,即/36174/se_enriquecio_elba_en_sexenios_del_pan.html
。现在您必须提供真实页面。为此,我们添加了一条附加规则,类似于您在问题中已有的规则
RewriteRule ^/?(.+?)/(.+?)\.html$ /article.php?id=$1&t=$2 [L]
但是现在,有一个无限的重定向循环。我们通过使用环境变量来打破这一点。这是整个完整的规则集
RewriteEngine On
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=(.+)$
RewriteRule ^/?article\.php$ /%1/%2.html? [R,L]
RewriteRule ^/?(.+?)/(.+?)\.html$ /article.php?id=$1&t=$2 [L,E=SEO:1]
只要未设置环境变量,此规则就会按上述方式进行重定向。并且它同时服务于真实页面article.php
并设置环境变量以防止循环。
您也可以为此目的使用 cookie。但是,如果在客户端中禁用了 cookie,那将会中断。