Reputation: 307
I am having a hard time finding a concrete answer to what I am searching. Mainly what I am working with is making pretty urls. For the most part I was able to make it work, when I know the ammount of variables that the url must contain, for example:
Original URL:
index.php?lang=en&show=contact-us
htaccess:
RewriteRule ^([a-z]+)/([a-z-]+)$ index.php?lang=$1&show=$2 [NC,L]
new URL:
/en/contact-us
And I have multiple ones as this one for the "known ammount of variables", I will show you another example:
original URL:
index.php?lang=en&show=for-rent-details&object=8500479
htacces:
RewriteRule ^([a-z]+)/for-rent/([0-9]+)/([0-9a-z-]+)$ index.php?lang=$1&show=for-rent-details&id=$2&name=$3 [NC]
new URL:
/en/for-rent/8500479/rocha-tower-14e
But the problem now is when the user will make a search. Meaning that the url will be composed depending on the ammount of items the client will choose. Per default there are 5 variables:
Original URL:
index.php?lang=en&show=filter&beachDistance=500&roomQty=2&sortRent=asc
htaccess:
RewriteRule ^([a-z]+)/([a-z-]+)/([0-9]+)/([0-9]+)/([a-z]+)$ index.php?lang=$1&show=$2&beachDistance=$3&roomQty=$4&sortRent=$5 [NC]
new URL:
/en/filter/500/2/asc
This is the "default" url pattern of the search function. If the user does not select anything, these are the default variables and respective values that are sent. But in case the user wants to select some additional information (cities or points of interest) then they will appear after the like this:
working url:
index.php?lang=en&show=filter&poi-1=1&poi-3=3&beachDistance=500&roomQty=2&sortRent=asc
I want it to be:
/en/filter/1/3/500/2/asc
while another search might have:
/en/filter/1/3/44/13/90/500/2/asc
and maybe another with the following variables:
/en/filter/1/3/9/2/500/2/asc
So this is what I mean by unknown "amount of variables", that htaccess won't know exactly the amount but I want it still to rewrite the url no matter what. I have read about the QSA flag rule but I am a little unsure on how to implement it.
Upvotes: 0
Views: 42
Reputation: 8171
If it doesn't know the amount of variables, it can't know what value to pass to what variable. You could explore RewriteMap
with an external program to handle the rewrite in a php script but at that point you could do it directly in your controller.
What you can do is "touch up" part of your url and pass the remaining query parameters since the resulting urls you propose aren't too friendly and there are too many combinations, and for that you can use QSA
as you mention. That's what the flag does, pass the original query string to the new url.
Using the following rule:
RewriteRule ^([a-z]+)/([a-z-]+)$ index.php?lang=$1&show=$2 [QSA]
And given the request /en/filter/?poi-1=1&poi-3=3&beachDistance=500&roomQty=2&sortRent=asc
you would get the rewritten url index.php?lang=en&show=filter&poi-1=1&poi-3=3&beachDistance=500&roomQty=2&sortRent=asc
.
If you didn't set the flag apache would discard the query string, since the rewritten url already has one. You can check the behaviour of the QS*
flags here.
This rule will conflict with your current two-parameter rule, so you could either remove the existing rule or move the new rule to the begining and make it more specific so it only applies to the search page:
RewriteRule ^([a-z]+)/(filter)$ index.php?lang=$1&show=$2 [QSA,L]
Upvotes: 1