0

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

1
  • Whats inside the brackets ends up in your variables. Try ^\w+=(\w+)$ Commented Jul 15, 2014 at 18:37

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

8 Comments

sweet, but what if the queries are not present? I get 404.
For which URL you get 404?
If I went to the index of a directory. ie: site.com/
Then this rule won't even fire since none of the conditions will meet that URI /. Do you have other rules?
No other rules, and everything is on the index.php file. I only need it for that file. index.php?test=name&test2=integer (with other variations of name & integer)
|

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.