.htaccess 是否可以使用 3 个相同的参数?
Are 3 identical parameters possible with .htaccess?
到目前为止,@anubhava 和 @RavinderSingh13 极大地帮助了我更多地了解 .htaccess 和重写规则。但是,即使第一条规则(带有一个参数:$id)和第二条规则(带有两个参数:$id 和 $name) 用于重写 url,我尝试使用两个参数($id 和 $ 的第三条规则class) 失败,并且不会重写最终的 url。到目前为止,以下是我的整个文件。我想补充一点,$class参数格式为a-class,b-class,等等,所以这可能是导致重写不起作用的原因?感谢您帮助我继续学习至今!
RewriteEngine on
--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= [L,QSA,NC]
--2nd rule with two parameters: $id and $name--
RewriteEngine on
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=&name= [NC,L,QSA]
--My attempt at the third rule following the example from the second, but this rule fails. Parameters are $id and $class--
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=&class= [NC,L,QSA]
根据您展示的示例并考虑到 class 单词将始终出现在您的第 3 url 中,请尝试遵循规则。另外你不需要多次写RewriteEngine ON,只需要1次启动文件就足够了。
RewriteEngine on
##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= [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=&class= [NC,L,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=&name= [NC,L,QSA]
@anubhava 和 @RavinderSingh13 极大地帮助了我更多地了解 .htaccess 和重写规则。但是,即使第一条规则(带有一个参数:$id)和第二条规则(带有两个参数:$id 和 $name) 用于重写 url,我尝试使用两个参数($id 和 $ 的第三条规则class) 失败,并且不会重写最终的 url。到目前为止,以下是我的整个文件。我想补充一点,$class参数格式为a-class,b-class,等等,所以这可能是导致重写不起作用的原因?感谢您帮助我继续学习至今!
RewriteEngine on
--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= [L,QSA,NC]
--2nd rule with two parameters: $id and $name--
RewriteEngine on
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=&name= [NC,L,QSA]
--My attempt at the third rule following the example from the second, but this rule fails. Parameters are $id and $class--
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=&class= [NC,L,QSA]
根据您展示的示例并考虑到 class 单词将始终出现在您的第 3 url 中,请尝试遵循规则。另外你不需要多次写RewriteEngine ON,只需要1次启动文件就足够了。
RewriteEngine on
##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= [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=&class= [NC,L,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=&name= [NC,L,QSA]