Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite with a lookup table

http://www.website.com/dir/123 to http://www.website.com/script.php?id=45

         

CliffR

8:33 pm on Jun 5, 2004 (gmt 0)

10+ Year Member



Hello,

I have a shopping site with about 1000 product pages indexed in the search engines in the format:

[website.com...]

where "123" is a product model number and there is no file extension. I am upgrading to a new PHP catalog/shopping cart system that uses record ids instead of model numbers in the URL to produce the equivalent page like:

[website.com...]

So in the new system, product model "123" will be found at record id 45. I'm wondering if it's possible to use mod_rewrite with a lookup table to redirect to the new product URL and return a 301 header. I don't want to do this with 1000 individual lines in a .htaccess file because I'm afraid of degrading server performance (I get around 5000 unique visitors daily).

I was reading about using a RewriteMap on Apache.org and all this seems a little over my head, but here's what I have so far:

Make a text file with the lookup info:

##
## map.txt -- rewriting map
##
## model number, record id
123 45
321 47
478 48
527 49

Then I put something like the following in a .htaccess file:

RewriteMap model-to-id txt:/path/to/file/map.txt
RewriteRule ^/model/(.*)$ /script.php?id=${model-to-id :$1} [R=301,L]

Am I on the right track? Will this send back a 301 response?

Best Regards,
CliffR

gergoe

10:43 am on Jun 6, 2004 (gmt 0)

10+ Year Member



Yes, you are on the right track. However your code can be improved a bit, because if there's request with a model id which does not have the corresponding id then the target php page might fail because of the empty id parameter. You can avoid this by using two RewriteConds:

[b]RewriteMap model-to-id txt:/path/to/file/map.txt[/b]
# First we put the model-id into a RewriteCond backreference
[b]RewriteCond %{REQUEST_URI} ^/model/(.+)$[/b]
# And now test the model->id relation, and rewrite the url only if there's a matching id.
[b]RewriteCond ${model-to-id:%1} !^$[/b]
# Now a 404 (or something else generated by the old cart system) is sent back to non-existing items, this can be changed by adding an other RewriteRule here to redirect the browsers to a catalog or somethig like that
[b]RewriteRule ^/model/(.+)$ /script.php?id=${model-to-id:$1} [R=301,L][/b]

Another workaround is to specify a default value for the RewriteMap like this:
RewriteRule ^/model/(.+)$ /script.php?id=${model-to-id:$1[b]¦0[/b]} [R=301,L]

but this might not be handled by the target php properly, because when Apache can't match the model-id then it substitutes it with 0 (of course you can change it), but this might cause failure also in the target php.

CliffR

10:44 pm on Jun 7, 2004 (gmt 0)

10+ Year Member



Hi gergoe,

Thanks so much. One other thing I'm not clear on is whether I can implement this in a .htaccess file. I was reading something that seemed to indicate that the RewriteMap can only be placed in the httpd.conf file. Is this your understanding as well?

Best Regards,
CliffR

jdMorgan

11:08 pm on Jun 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> RewriteMap can only be placed in the httpd.conf file

Correct. The documentation of the RewriteMap directive specifies that it can be used in server config and virtual host contexts only.

Apache Links
Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions Tutorial [etext.lib.virginia.edu]

I would suggest that you consider making the new id number equal to the original model number, so that the numeric part of the URL does not need to be 'mapped'. The other alternatives are to use a script and database to do the lookup, or to change hosting to a server that gives you httpd.conf access (this would be a dedicated server, and costly).

Jim

gergoe

2:37 am on Jun 8, 2004 (gmt 0)

10+ Year Member



... to change hosting to a server that gives you httpd.conf access (this would be a dedicated server, and costly).

This must not be true for all the hosting providers, I know a company who does this kind of things (without forcing you to put up a dedicated server), so you can keep looking for such a service. Might be a long run, but you would be better of at the end.