MysteryDev
MysteryDev

Reputation: 638

.htaccess redirect query strings

This was kind of hard to search for, I found examples of other work but basically I have a url:

site.com/?page=test&id=3

I want to rewrite as: site.com/test:3

with 'test' being page, and 3 being id.

I have the following match for equal values:

RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ /%1:%2?

But that only works for the key and the value, what regex can I use to select just the key, with %1 and %2 ?

Thanks,

My Second Attempt: (I only need on index.php)

RewriteCond %{QUERY_STRING} ^page=(\w+)&id=([0-9]*)$
RewriteRule ^index\.php$ /%1:%2**?** [R=302,L]

but now a 404 error is occuring when I do: index.php/details:1

Upvotes: 0

Views: 25

Answers (1)

anubhava
anubhava

Reputation: 784958

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{THE_REQUEST} \?test=([^\s&]+)&test2=([^\s&]+) [NC]
RewriteRule ^/?$ /%1:%2? [R=302,L,NE]

RewriteRule ^([^:]+):([^/]+)/?$ /?test=$1&test2=$2 [L,QSA]

Upvotes: 1

Related Questions