0

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

1

2 Answers 2

2

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.

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.