RewriteRule for the dynamic part of url which is not permanent

57 Views Asked by At

I have a

RewriteRule ^([^/]+)/id([0-9]+)/?$ /?page=$1&id=$2&location=$3 [L]

I need to make a rule for the part &location=

This part of url &location= is not permanent. The url originally presented without &location= but some links will include with part and I need to add it to the RewriteRule.

I was trying to do this way

RewriteRule ^([^/]+)/id([0-9]+)/([0-9]+)/?$ /?page=$1&id=$2&location=$3 [L]

But in this case the url like this

http://www.example.com/travel/id725

not found without 3-rd parameter. But I need it to work both ways

http://www.example.com/travel/id725
AND
example.com/travel/id725/43

Can you help me with it please?

1

There are 1 best solutions below

1
On BEST ANSWER

You can add two different rules. First, when all three parameters are present; and then for the case where only two parameters are present.

RewriteRule ^([^/]+)/id(\d+)/(\d+)/?$ /?page=$1&id=$2&location=$3 [L]
RewriteRule ^([^/]+)/id(\d+)/?$ /?page=$1&id=$2 [L]