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.
views:
39answers:
3
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
2010-07-05 19:01:54
That does seem to be working:The requested URL was not found on this server.
werd
2010-07-05 19:56:37
Do you have a page that responds to `site.com/lv/1/2`? Are you using a framework of some kind?
Matt S
2010-07-05 20:57:00
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
2010-07-05 19:02:00
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
2010-07-06 07:01:58
None of these is working any way, is there something else I need to change in Apache configuration or other?
werd
2010-07-06 11:39:57
@werd: Are you using the obligatory `RewriteEngine on`? And where do you want to use that rule?
Gumbo
2010-07-06 12:14:43
Yes I am using it, I am using it on site index. In root directory I have index.php and .htaccess files
werd
2010-07-06 14:11:16
@werd: And the URL path is always just `/` and there are always these three URL parameters all having a value?
Gumbo
2010-07-08 18:16:26