views:

1062

answers:

3

Is there a way to put a rewrite rule in htaccess that will convert query strings to segmented URIs? For eg.

http://domain.com/controller/method/?a=1&b=2&c=3

should get rewritten as

http://domain.com/controller/method/a/1/b/2/c/3

All there is to do is remove the ? and replace the &s and the =s with a /, but I'm not quite sure how all that figures into .htaccess...

Not that it'd matter, but I'm using PHP and CodeIgniter on Apache.

+1  A: 

Why don't you put that logic inside your CodeIgniter controller instead of doing it in .htaccess ?

Check CodeIgniter's User Guide for handling Query Strings and URLs.

duckyflip
I'm having trouble with CodeIgniter receiving the query strings from the URL. It works fine on the local server, but on the live server, the moment I put anything after a `?` at the end simply makes CI throw up a 404. That's why I want to rewrite the query string into a segmented URI so CI doesn't throw it's hands up.
aalaap
@aalaap you can enable query strings in CI by setting $config['enable_query_strings'] = TRUE; check this: http://codeigniter.com/user_guide/general/urls.html
duckyflip
A: 

If you want to do it in the .htaccess you need to have mod_rewrite.

The rule is not too difficult. I give you a minimal example, and you need to tune the regex up to fix your need.

RewiteRule ^/([a-z]+)/([a-z]+)$ /method/?$1=$2
Jlbelmonte
I need to do the exact opposite of what you're doing with this rule!
aalaap
A: 

This is what you could do:

RewriteCond %{THE_REQUEST} ^GET\ /[^?]*\?
RewriteCond %{QUERY_STRING} ^([^&=]+)=?([^&]*)&([^&=]+)=?([^&]*)&?(.*)
RewriteRule ^ %{REQUEST_URI}/%1/%2/%3/%4?%5 [L]
RewriteCond %{THE_REQUEST} ^GET\ /[^?]*\?
RewriteCond %{QUERY_STRING} ^([^&=]+)=?([^&]*)&?(.*)
RewriteRule ^ %{REQUEST_URI}?/%1/%2?%3 [L]
Gumbo
Wouldn't there be a way to rewrite the URLs without a redirect? I mean, that's what `mod_rewrite` is for, right! What would happen if I was to drop the `R=301` from the rules you put down?
aalaap
Of course there are. Just leave the R flag away.
Gumbo