问题描述
@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]