微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

重定向 - 总共 4 个参数

如何解决重定向 - 总共 4 个参数

@anubhava@ravinderSingh13 通过这个脚本继续帮助我学习并取得今天的进展。这是我目前所拥有的:

##1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.PHP$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.PHP?id=$1 [L,QSA,NC]

##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.PHP\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.PHP?id=$1&class=$2 [NC,QSA]

##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.PHP\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.PHP?id=$1&name=$2 [NC,QSA]```

现在,我想添加一个只有一个参数而不是两个参数的规则。但是,此参数在 url 中使用 PHP $_GET['name']。我正在尝试添加一个规则,将 http://IPADDRESS.com/file.php?name=JohnDoe 重写为 http://IPADDRESS.com/newdirectory/JOHNDOE

谢谢!

解决方法

对于您显示的样本,请尝试以下操作。在测试您的网址之前,请务必清除浏览器缓存。

RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]

##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,QSA]

##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,QSA]

##Rule with two parameters: $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?name=(\S+)\s [NC]
RewriteRule ^ /directory/%1? [R=301,L]
RewriteRule ^directory/([^/]*)/?$ /directory/file.php?name=$1 [NC,QSA]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。