views:

39

answers:

3

Hi, I would like to know how can I rewrite www.site.com?lang=lv&type=1&mode=2 into www.site.com/lv/1/2 using Apache mod_rewrite options.

A: 

Basic rewrite:

RewriteEngine On
RewriteRule http://www.site.com/?lang=(.+?)&type=(\d+?)&mode=(\d+?) http://www.site.com/$1/$2/$3 [L,R=permanent]

Edit: Changed rewrite flags to force a permanent redirect

Matt S
That does seem to be working:The requested URL was not found on this server.
werd
Do you have a page that responds to `site.com/lv/1/2`? Are you using a framework of some kind?
Matt S
A: 

Assuming that you actually want to rewrite /lv/1/2 to ?lang=lv&type=1&mode=2 (I see no reason to do the opposite) and that no other rewrites are active, this should do the trick:

RewriteRule ^/([^/]*)/([^/]*)/([^/]*)$ ?lang=$1&type=$2&mode=$3 [L]

Also; you'd be better off replacing those magic numbers with more useful information if you want to include them in your URI.

Edit: If it really is the opposite you'd like to do, see the answer by Matt S.

You
Yes, I ment the opposite :P Thank you!
werd
A: 

You need to handle the URI path and query separately. If the parameters appear in that very same order, you can do this:

RewriteCond %{QUERY_STRING} ^lang=([^&]+)&type=([^&]+)&mode=([^&]+)$
RewriteRule ^$ /%1/%2/%3? [L,R=301]

Otherwise you will need to put them in the right order before using this rule or take each parameter at a time.

Gumbo
None of these is working any way, is there something else I need to change in Apache configuration or other?
werd
@werd: Are you using the obligatory `RewriteEngine on`? And where do you want to use that rule?
Gumbo
Yes I am using it, I am using it on site index. In root directory I have index.php and .htaccess files
werd
@werd: Are you using any other rules that can conflict with this one?
Gumbo
no, I am not, just 1:1 you wroted
werd
@werd: And the URL path is always just `/` and there are always these three URL parameters all having a value?
Gumbo