RewriteRule - two parameters but last parameter is optional

久未见 提交于 2020-01-11 04:45:08

问题


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.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.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.


回答1:


Try using this rule instead:

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

Hope that helps



来源:https://stackoverflow.com/questions/5809602/rewriterule-two-parameters-but-last-parameter-is-optional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!