How do I write a .htaccess rewrite rule to convert query strings to sharded URIs?

Is there a way to set a rewrite rule in htaccess that converts query strings to sharded URIs? For instance,

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

      

should be rewritten as

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

      

All you have to do is uninstall? and replace & s and = s with /, but I'm not entirely sure how this all figured out in .htaccess ...

Not that it matters, but I am using PHP and CodeIgniter in Apache.

0


a source to share


3 answers


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



Check out the CodeIgniter User Guide for handling string strings and request urls.

+1


a source


If you want to do this in .htaccess you need to have mod_rewrite.

The rule is not too hard. I am giving you a minimal example and you need to tweak the regex to fix your needs.



RewiteRule ^/([a-z]+)/([a-z]+)$ /method/?$1=$2

      

0


a source


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]

      

0


a source







All Articles