mauro mauro - 19 days ago 5
Apache Configuration Question

htaccess: rewrite and redirect 301

I rewrited, by .htaccess, a category of dynamic url generated by a query string in this mode:

RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]


Now my rewrite works in right way and, for example, the following urls drive to the same page:

http://www.mysite.it/id1-01234-id2-56789
http://www.mysite.it/page.php?id1=01234&id2=56789


But now I want a redirect 301, from second type to first type, for all dynamic urls. For example:

from

http://www.mysite.it/page.php?id1=01234&id2=56789


to

http://www.mysite.it/id1-01234-id2-56789


The following way doesn't work:

RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
RewriteCond %{QUERY_STRING} (^|&)id1=$1($|&)
RewriteCond %{QUERY_STRING} (^|&)id2=$2($|&)
RewriteRule ^page\.php$ /id1-id2? [L,R=301]


Where is the error?

can you help me please?

Answer
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/page.php
RewriteCond %{THE_REQUEST} \?id1=(\w+)&id2=(\w+)\s
RewriteRule ^page.php /id1-%1-id2-%2? [NC,R=301,L]

RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id2=$2 [L]