Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewriteRule - two parameters but last parameter is optional

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.

like image 727
MacMac Avatar asked Apr 27 '11 19:04

MacMac


1 Answers

Try using this rule instead:

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

Hope that helps

like image 175
clmarquart Avatar answered Oct 23 '22 22:10

clmarquart