Rewrite with multiple parameters!?

I’m trying to get this to work but with no luck?

I have this url:
oldsite.com/cleverlink?key1=123456-10&key2=1234567890

And I want it to end up like this:
newsite.com/search/123456-10 1234567890

I’m trying with this and then comes up with an Page is not found…

RewriteRule (.*)cleverlink\?key1=([A-Za-z0-9]+)&key2=([0-9]+)$ newsite.com/search/$1%$2

What am I doing wrong???

Thanks in advance!

You can’t put query strings in RewriteRule, you need to put them in RewriteCond:

RewriteCond %{QUERY_STRING} .*(?:^|&)key1=(.*?)(?:&|$).*
RewriteCond %{QUERY_STRING} .*(?:^|&)key2=(.*?)(?:&|$).*
RewriteRule ^cleverlink$ https://newsite.com/%1/%2 [L,R=302]

Once you’ve tested that this works you should replace 302 with 301.

Thanks for answer, but i only get the second entry in the redirection:

newsite.com/2/?key1=1&key2=2

Ah yes, I forgot, you can only take values from the last RewriteCond :frowning:

In that case you should do something like this:

RewriteCond %{QUERY_STRING} .*(?:^|&)key1=(.*?)&(?:.*?&)?key2=(.*?)(?:&|$).*
RewriteRule ^cleverlink$ https://newsite.com/%1/%2 [L,R=302]

RewriteCond %{QUERY_STRING} .*(?:^|&)key2=(.*?)&(?:.*?&)?key1=(.*?)(?:&|$).*
RewriteRule ^cleverlink$ https://newsite.com/%2/%1 [L,R=302]

Try using the below code:
You can use the following Rule :

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([0-9a-zA-Z]+)(?:/([0-9]*))?/?$ index.php?pagetitle=$1&articleid=$2 [NC,L,QSA]

That would be true if the OP was going from “fancy URLs” to query string, but they’re going the other way around. The code you suggested has nothing to do with that.

1 Like

How can i remove

?key1=1&key2=2
in url :
https://newsite.com/1/2?key1=1&key2=2

by same i would like to not have 1/2 but 1 space 2, is it possible ?

Change to:

RewriteCond %{QUERY_STRING} .*(?:^|&)key1=(.*?)&(?:.*?&)?key2=(.*?)(?:&|$).*
RewriteRule ^cleverlink$ https://newsite.com/%1/%2? [L,R=302]

RewriteCond %{QUERY_STRING} .*(?:^|&)key2=(.*?)&(?:.*?&)?key1=(.*?)(?:&|$).*
RewriteRule ^cleverlink$ https://newsite.com/%2/%1? [L,R=302]

Yes, but I leave that as an exercise to reader. It really shouldn’t be hard to figure that one out :slight_smile:

1 Like

You’re amazing ! Thanks a lot.
here’s my modification :

RewriteCond %{QUERY_STRING} .*(?:^|&)key1=(.*?)&(?:.*?&)?key2=(.*?)(?:&|$).*
RewriteRule ^cleverlink$ "http://newsite/search/%1\ %2?" [L,R=301]

am i right ?

I don’t think you’d need the double quotes, but yes, that should work :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.