Rickstar
Rickstar

Reputation: 6189

.htaccess file url rewrite keeps going to the wrong page

I am trying to get the follow urls to work

www.urlname.com/team/1-Test

www.urlname.com/team/1-Test/members

RewriteRule ^team/([^-]+)-([^&]+)$ index.php?p=teamprofile&team_name=$2&team_id=$1 
RewriteRule ^team/([^-]+)-([^&]+)/members$ index.php?p=teammembers&team_name=$1&team_id=$2

but when i try the link with /members init it goes to the other page?

can someone help me please

Thanks

Upvotes: 0

Views: 98

Answers (2)

Chris Baker
Chris Baker

Reputation: 50592

The trouble is, your second rule is being satisfied by the first rule. You could simply switch them around and it will work:

RewriteEngine On
RewriteRule ^team/([^-]+)-([^&]+)/members$ index.php?p=teammembers&team_name=$1&team_id=$2
RewriteRule ^team/([^-]+)-([^&]+)$ index.php?p=teamprofile&team_name=$2&team_id=$1 

Although, a slight change in the first rule will also address the problem:

RewriteEngine On
RewriteRule ^team/([^-]+)-([^/]+)[/]?$ index.php?p=teamprofile&team_name=$1&team_id=$2 [L]
RewriteRule ^team/([^-]+)-([^/]+)/members[/]?$ index.php?p=teammembers&team_name=$1&team_id=$2 [L]

Note, I changed the match string in the first rule from ([^&]+) to ([^/]+) - that way the forward slash isn't included in the match in cases like mydomain.com/team/1-2/. The [/]? rule at the end is an optional match for that trailing forward slash. I've likewise added one to the end of the members rule as well, now it works like this:

mydomain.com/team/1-2/ - goes to index.php?p=teamprofile&team_name=1&team_id=2 mydomain.com/team/1-2 - goes to index.php?p=teamprofile&team_name=1&team_id=2 mydomain.com/team/1-2/members - goes to index.php?p=teammembers&team_name=1&team_id=2 mydomain.com/team/1-2/members/ - goes to index.php?p=teammembers&team_name=1&team_id=2

Upvotes: 0

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

[^-] and [^&] includes the / so /members is included with that. you could either add / to your negation character groups like [^-/] and [^&/] so it doesn't match / or move the bottom one up and add [L] after it to tell apache this is the [L]ast rule to check if it matches.

Upvotes: 2

Related Questions