Forum Moderators: phranque
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
[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]
RewriteRule ^/model/(.+)$ /script.php?id=${model-to-id:$1[b]¦0[/b]} [R=301,L]
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
... to change hosting to a server that gives you httpd.conf access (this would be a dedicated server, and costly).