Rewrite URL with RewriteMap Error

125 Views Asked by At

I have to Rewrite massive number of URLs e.g: FROM: http://www.lespoulettes-bijoux.fr/produit2.asp?id_produit=8489 TO: http://www.lespoulettes-bijoux.fr/colliers-femme-createur-fantaisie/femme-collier-ras-de-cou-fils-d-argent-et-6-perles-blanches-et-grises-10005.html

So I selected to do a RewriteMap cause my URL doesn't have the same ID to match with.

So I declared my RewriteMap who is loking like that:

8489 http://www.lespoulettes-bijoux.fr/colliers-femme-createur-fantaisie/femme-collier-ras-de-cou-fils-d-argent-et-6-perles-blanches-et-grises-10005.html

In my .htacess: I wanted to convert my URL to transform my GET parameter (id_produit) into an interal id to git it after in my ReWriteMap.

The Problem: I have An Error 500 while converting my URL and I dont know the right syntax to switch after convert it:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id_produit\=(.+)$
RewriteRule (.*) /$1/id=%1 <== HERE 500 Internal Server Error
#RewriteRule ^/id_produit.asp/(.*)$ ${map_product:$1} [R]

To resume: I want to switch 'www.exemple.com/produit2.asp?id_produit=8489' into www.exemple.com/produit2.asp/id=8489 and send the id to my RewriteMap to redirect to the Right URL.

Thanks for your help!

2

There are 2 best solutions below

0
On

I almost have the solution:

RewriteCond %{QUERY_STRING} ^id_produit\=(.+)$
RewriteRule (.*) /$1/%1? [L]
RewriteRule ^produit2.asp/(.*)$ ${map_product:$1} [R,L]

Redirect me into my map but add /var/www/les-poulettes-bijoux/ in the middle of my URL:

/produit2.asp?id_produit=8489 => http://www.lespoulettes-bijoux.fr/var/www/les-poulettes-bijoux/colliers-femme-createur-fantaisie/femme-collier-ras-de-cou-fils-d-argent-et-6-perles-blanches-et-grises-10005.html

But i need http://www.lespoulettes-bijoux.fr/colliers-femme-createur-fantaisie/femme-collier-ras-de-cou-fils-d-argent-et-6-perles-blanches-et-grises-10005.html

How can I delete that useless part in my URL?

1
On

The problem here is probably:

RewriteCond %{QUERY_STRING} ^id_produit\=(.+)$
RewriteRule (.*) /$1/id=%1 <== HERE 500 Internal Server Error

that is causing a loop. The rewrite engine loops so after this is rewritten, the result will still match this rule and thus get rewritten again. Try adding a ? to the end so that the query string is removed:

RewriteCond %{QUERY_STRING} ^id_produit\=(.+)$
RewriteRule (.*) /$1/id=%1? [L]

Additionally, yuo need to remove the leading slashes in the regex. Request URI's have their leading slashes stripped off in rules that are per-directory based (like in an htaccess file) so the regex needs to look like:

RewriteRule ^id_produit.asp/(.*)$ ${map_product:$1} [R,L]

And this rule needs to be before your other rule.