apache - 将参数url重写为普通url,但重定向后应该获取原始url参数

标签 apache .htaccess redirect mod-rewrite url-rewriting

原网址: 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/

相关文章:

wordpress - mod deflate 不压缩根目录的子目录

php - Codeigniter + CSS 不能正常工作,可能是 baseurl

http - 创建框架重定向会导致 Chrome 抛出 X-Frame-Options 错误

php - .htaccess 重定向到整洁的 URL

apache - CodeIgniter 解析错误显示但未写入日志

oracle - perl + DBD::Oracle + mod_perl + oracle LDAP 名称解析器 = 崩溃?

apache - 未定义对 'uuid_generate@UUID_1.0 的引用

apache - 将所有 URL 重定向到 httpd.conf 中的单个页面

php - 漂亮链接中的撇号和冒号

windows-7 - 命令行语法可防止在 PowerShell 输出文件中换行?