Question

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?

Was it helpful?

Solution

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]

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top