0

I'm currently trying to rewrite some URLs, and they all follow a basic format.

Here is the current .htaccess rule

RewriteRule ^([A-Za-z]+)/([A-Za-z]+)$ index.php?d=$1&n=$2 [QSA] 

Sometimes, there are some links, such as

/users/profiles/userid=2

But, the rule is written to rewrite

/users/profiles

as

index.php?d=users&n=profiles

How can I change the rule so that it can optionally accept and add extra parameters?

2 Answers 2

1

I haven't tested, but I think this should work:

RewriteRule ^([A-Za-z]+)/([A-Za-z]+)/(.*)$ index.php?d=$1&n=$2&$3 [QSA] 

This:

/users/profiles/userid=2&something=3

should become like this

index.php?d=users&n=profiles&userid=2&something=3

Sign up to request clarification or add additional context in comments.

2 Comments

That kills it, if there is no additional parameter.
You should combine this rule with the previous rule. @LazyOne has explained you everything :)
0

The rule below will rewrite users/profiles/userid=2 to index.php?d=users&n=profiles&userid=2. Tested, working fine.

RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+)=(\d+)$ index.php?d=$1&n=$2&$3=$4 [NC,QSA,L] 

If you add the above rule above your existing rule they both will play nicely (I have modified your existing rule a bit):

RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+)=(\d+)$ index.php?d=$1&n=$2&$3=$4 [NC,QSA,L] RewriteRule ^([a-z]+)/([a-z]+)$ index.php?d=$1&n=$2 [NC,QSA,L] 

1 Comment

Do not delete your previous rule, just add this one below (or above) -- see updated answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.