{"id":331,"date":"2014-10-03T14:51:54","date_gmt":"2014-10-03T14:51:54","guid":{"rendered":"http:\/\/www.nicovs.be\/?p=331"},"modified":"2014-10-05T11:53:04","modified_gmt":"2014-10-05T11:53:04","slug":"apache-rewritemap-example","status":"publish","type":"post","link":"https:\/\/www.nicovs.be\/?p=331","title":{"rendered":"Apache RewriteMap example"},"content":{"rendered":"<p>Today I needed to rewrite a list of about 1000 URL&#8217;s to a new location. Our product database was cleaned and new URL&#8217;s were created&#8230; Using RewriteMap from Apache&#8217;s Mod_rewrite comes in handy here.<\/p>\n<p>In my case I needed to rewrite URL&#8217;s like this:<\/p>\n<p>www.exampledomain.com\/en\/product1234\/product_detail.php<\/p>\n<p>to<\/p>\n<p>www.exampledomain.com\/en\/travelbox_12\/product_detail.php<\/p>\n<p>Where in my URL&#8217;s I had a 1000 different &#8216;poducts1234&#8217; to rewrite to a new URL.<\/p>\n<p>&nbsp;<\/p>\n<p>Here&#8217;s the HOWTO:<\/p>\n<p>Create a textfile on your server with 2 columns. Something that would look like this:<\/p>\n<pre class=\"lang:default decode:true \">product1234 travelbox_34\r\nproduct1235 travelbox_13\r\nproduct2310 case_07\r\nproduct3126 case_12\r\nproduct9320 anotheritem_54\r\n....\r\n....\r\n<\/pre>\n<p>And save it to a folder you wish, eg<\/p>\n<pre class=\"lang:default decode:true\">vim \/etc\/apache2\/conf.d\/productrewrites.txt<\/pre>\n<p>from this file, \u00a0create a rewrite database, \u00a0more info: <a title=\"httxt2dbm\" href=\"http:\/\/httpd.apache.org\/docs\/2.2\/programs\/httxt2dbm.html\" target=\"_blank\">httxt2dbm<\/a><\/p>\n<pre class=\"lang:default decode:true\">httxt2dbm -f db -i \/etc\/apache2\/conf.d\/productrewrites.txt -o \/etc\/apache2\/conf.d\/productrewrites.db<\/pre>\n<p>Once done, \u00a0open your vhosts config and add the following configuration:<\/p>\n<pre class=\"lang:default decode:true\">RewriteEngine On\r\nRewriteMap rewrites dbm=db:\/etc\/apache2\/conf.d\/productrewrites.db \r\nRewriteCond ${rewrites:$1} !=\"\"\r\nRewriteRule ^\/(.*)\/(.*)\/(.*)$ http:\/\/www.exampledomain.com\/(.*)\/${rewrites:$1}\/$2? [R=permanent,L] \r\n\r\n<\/pre>\n<p>A bit of clarification:<\/p>\n<p>The line with content:<\/p>\n<pre class=\"lang:default decode:true\">RewriteCond ${rewrites:$1} !=\"\"<\/pre>\n<p>creates a condition that if the rewritemap contains the URI and the value is not &#8220;&#8221; (so an empty string) then it should perform the <code>RewriteRule<\/code>.<\/p>\n<p>The rewrite rule itself is pretty obvious. It&#8217;ll rewrite the URL&#8217;s for which the rewritemap is found to the new product URL, and also keep the trailing parameters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I needed to rewrite a list of about 1000 URL&#8217;s to a new location. Our product database was cleaned and new URL&#8217;s were created&#8230; Using RewriteMap from Apache&#8217;s Mod_rewrite comes in handy here. In my case I needed to rewrite URL&#8217;s like this: www.exampledomain.com\/en\/product1234\/product_detail.php to www.exampledomain.com\/en\/travelbox_12\/product_detail.php Where in my URL&#8217;s I had a 1000 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[79,20,4,45,44,19,6],"tags":[],"class_list":["post-331","post","type-post","status-publish","format-standard","hentry","category-apache","category-centos","category-debian","category-linux","category-php","category-red-hat","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=331"}],"version-history":[{"count":5,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions"}],"predecessor-version":[{"id":336,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions\/336"}],"wp:attachment":[{"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nicovs.be\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}