rewriteMap: continue as usual when nothing found

Discuss practical ways rearrange URLs using mod_rewrite.

rewriteMap: continue as usual when nothing found

Postby slowinthehead » Fri Oct 02, 2009 4:33 am

Hi,

i am currently preparing a shop-move (xt:commerce). The Shop uses SEF-URLs (shopstat).

In the new shop the link-structure will change so i thought i will build a rewrite map to map old URLs to new ones.

So currently there already is a rewrite going on:

Code: Select all
RewriteRule (.*)::(.+)\.html$ product_info.php?products_id=$2 [qsappend,L]


This will apply for example to

Code: Select all
http://www.example.com/category_name/product_name::1234.html


Now the NEW URL to the same product in the new shop could be completely different:

Code: Select all
http://www.example.com/other_category_name/other_product_name::5678.html


So i would put it in the map like this:

Code: Select all
1234 /other_category_name/other_product_name::5678.html


Now, here is my rewrite rule to access the rewritemap:

Code: Select all
RewriteRule ^.*::(.*)\.html$ ${remap:$1} [R=301,L]


Unfortunately this doesn't work. The url ends up with:

Code: Select all
http://www.example.com/path/to/my/doc_root/


On the other hand, this thing works, when i point the target in the rewritemap to a REAL existing url:

Code: Select all
1234 /thisfiledoesexist.php


Which is not what i want obviously.

How can i get this to work:

1. lookup in rewrite map if entry exists
2. if yes, redirect (301) to the new target which will be rewritten properly
3. if not, just keep going to the next rule which will rewrite to the products.php

Here is the .htaccess to sum it up:

Code: Select all
# rule no. 1
# should lookup if entry in rewritemap exists
# if yes, redirect to found target and go to rule no. 2
RewriteRule ^.*::(.*)\.html$ ${remap:$1} [R=301,L]

# rule no.2
# just map to the correct product
RewriteRule (.*)::(.+)\.html$ product_info.php?products_id=$2 [qsappend,L]


Sorry for being noobish but i am going crazy over this (many hours spent) and kinda lost it.

Please help.

Thank you.
slowinthehead
 
Posts: 2
Joined: Fri Oct 02, 2009 4:08 am

Postby richardk » Sat Oct 03, 2009 8:11 am

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteMap remap txt:/path/to/remap.map

# Search in the map.
RewriteCond ${remap:$1|not-found} ^(.+)$
# Make sure it was found.
RewriteCond %1 !^not-found$
RewriteRule ^.+::([0-9]+)\.html$ %1 [R=301,L]

RewriteRule ^.+::([0-9]+)\.html$ /product_info.php?products_id=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby slowinthehead » Mon Oct 05, 2009 12:59 am

Amazing! Works perfect!

Thank you!!!
slowinthehead
 
Posts: 2
Joined: Fri Oct 02, 2009 4:08 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron