views:

34

answers:

2

I have a problem with rewrite rule

my link is www.something/group/group_id/place/groupName for this rewriteBase /

RewriteRule ^group/(.*)/(.*)/(.*)$ /group.php?gid=$1 [QSA,NC,L]

somet times my url may come www.something/group/group_id/groupName.

In Both cases I have to rewrite to group.php and I need only groupid. How to write rewrite rule to work in both situation?

A: 

Try this one:

^group/(.+)(/|/.+)*$

It matches for
www.something/group/group_id/place/groupName
www.something/group/group_id/groupName

I never used the RewriteRule, so it's not tested. And maybe if you add the "Regex" Tag to your question you'll get more answers ;-)

Philipp G
A: 

Either use lazy quantifiers or prevent each matching group from matching the / itself. The way you have it currently, the first group will match as much as it can resulting in unwanted results.

RewriteRule ^group/(.*?)/(.*?)/(.*?)$ /group.php?gid=$1 [QSA,NC,L]
RewriteRule ^group/([^\/]*)/([^\/]*)/([^\/]*)$ /group.php?gid=$1 [QSA,NC,L]

An even better way, to allow people to leave out unnecessary parts (read: not needed to evaluate the result on the server side), you could even do something like this:

 RewriteRule ^group/(\d+)(/.*)?$ /group.php?gid=$1 [QSA,NC,L]

(This is based on the assumption that your group id is a number)

poke
thanks poke. It is working. I applied your last suggestion
roopesh
Good to hear, but please accept the answer then to mark this question as resolved :)
poke