views:

22

answers:

2

I am trying to combine these four get variables (sch, val, lmt, ord) into a nice looking url re_write. Currently I have the following in my .htaccess file and it works if all variables are included, however, if I only input the first two variables (sch & val) it breaks:

RewriteRule ^search/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ 
             search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]

www.domain.com/search/City/London/5/asc works www.domain.com/search/City/London but this doesn't

+2  A: 

Why not just add the other rewrite rules ahead of it. IE:

 RewriteRule ^search/([^/]*)$ 
         search.php?sch=$1 [L]
 RewriteRule ^search/([^/]*)/([^/]*)$ 
         search.php?sch=$1&val=$2 [L]
 RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)$ 
         search.php?sch=$1&val=$2&lmt=$3 [L]
 RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)/([^/]*)$ 
         search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]

That should work. Not sure if it is what you want, but I do not really think it is "possible" to do that without just doing a query and passing the full query string as one line then parsing it PHP side.

EDIT

The nice thing about doing all this in .htaccess vs PHP is it requires less logic and you pass what you want to your script in the correct values. My preference would be using the .htaccess above.

Brad F Jacobs
+1  A: 

you can use this , to grab whole path as text and parse www.domain.com/search/City/London/5/asc works www.domain.com/search/City/London but this doesn't

RewriteRule ^search/(.*)$ search.php?url=$1
$paths=explode('/',$_GET('url'));

output
www.domain.com/search/City/London/5/asc

$paths=city,london,5,asc

www.domain.com/search/City/London

$paths=city,london

You can use loop and pair as key/value... if needed

JapanPro