原网址: https://www.example.com/all_users/user.php?id=1&subtype=p
预期网址: https://www.example.com/user/john-doe.html
我尝试使用以下规则,它重定向到预期的 URL,但我无法将参数(id 和子类型)与新 URL 一起使用。
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id\=1&subtype\=p$
RewriteRule ^all_users/user\.php$ https://www.example.com/user/john-doe.html? [R=301,L]
如何将参数值与新 URL 一起使用。
示例:
重定向后,新的 URL 将为 https://www.example.com/user/john-doe.html 然后我应该能够使用新的 URL 获取原始参数值。
$id = $_GET['id'];
$type = $_GET['subtype'];
最佳答案
根据您显示的示例和尝试,请尝试遵循 htaccess 规则。
在测试您的网址之前,请确保清除浏览器缓存。
RewriteEngine ON
##External redirect here.
RewriteCond %{THE_REQUEST} \s/all_users/user\.php\?id=\d+&subtype=p\s [NC]
RewriteRule ^ user/john-doe.html? [R=301,L]
##Internal rewrite from here..
RewriteRule ^user/john-doe\.html/?$ all_users/user.php?id=1&subtype=p [QSA,NC,L]
关于apache - 将参数url重写为普通url,但重定向后应该获取原始url参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70470693/