RewriteRule - 两个参数但最后一个参数是可选的

时间:2022-06-01 14:09:59

I cannot rewrite to include two parameters but the last parameter is optional, so for example:

我无法重写包含两个参数,但最后一个参数是可选的,例如:

http://www.mywebsite.com/friends/jamie - (the forward slash should be optional too). Which should be the same as this:

http://www.mywebsite.com/friends/jamie - (正斜杠也应该是可选的)。哪个应该与此相同:

http://www.mywebsite.com/friends.php?name=jamie

When including a second parameter

包含第二个参数时

http://www.mywebsite.com/friends/jamie/30 - Should be the same as this:

http://www.mywebsite.com/friends/jamie/30 - 应该与此相同:

http://www.mywebsite.com/friends.php?name=jamie&page=30

This rule does not work:

此规则不起作用:

RewriteRule ^friends/(.*)/(.*)$ friends.php?name=$1&page=$2

Because I get this: The requested URL /friends/jamie was not found on this server. but works if I include a page number for the second parameter, so basically the second parameter should be optional.

因为我得到了:在此服务器上找不到请求的URL / friends / jamie。但如果我包含第二个参数的页码,则有效,所以基本上第二个参数应该是可选的。

1 个解决方案

#1


17  

Try using this rule instead:

请尝试使用此规则:

RewriteRule ^friends/([^/]*)/?(.*)$ friends.php?name=$1&page=$2

Hope that helps

希望有所帮助

#1


17  

Try using this rule instead:

请尝试使用此规则:

RewriteRule ^friends/([^/]*)/?(.*)$ friends.php?name=$1&page=$2

Hope that helps

希望有所帮助