1

I need to redirect (301) hundreds of old unstructured urls to new ones. Old urls have querystring domain/directories/randomtext?args=1

I have a file with oldurl newurl lines.

I am trying with RewriteMap in virtualhost

RewriteMap mapfile txt:mapfile.txt
RewriteCond ${mapfile:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.+)$ ${mapfile:$1} [L,R=301]

Is it possible to include Querystring in RewriteCond?

I solved this way:

#define map file with oldurl newurl format
RewriteMap mapfile txt:mapfile.txt
#test if request_uri?query_string is in mapfile so length is greater than ""
RewriteCond ${mapfile:$1?%{QUERY_STRING}} >""
#If matched rewrite oldurl to mapfile[key]
RewriteRule ^/(.+)$ ${mapfile:$1?%{QUERY_STRING}}? [R=301]

I think this could be improved because cache lookup always fails. I suppose only request_uri is cached.

RewriteLog

(2) init rewrite engine with requested uri /prueba
(3) applying pattern '(.*)' to uri '/prueba'
(4) RewriteCond: input='/prueba' pattern='^/$' => not-matched
(3) applying pattern '(.*)' to uri '/prueba'
(4) RewriteCond: input='/prueba' pattern='^/index.php$' => not-matched
(3) applying pattern '^/(.+)$' to uri '/prueba'
(4) RewriteCond: input='/prueba' pattern='!-f' => matched
(4) RewriteCond: input='/prueba' pattern='!-d' => matched
(4) RewriteCond: input='/prueba' pattern='!.*\.(ico|gif|jpg|jpeg|png|js|css|GIF|JPG)' =>     matched
(6) cache lookup FAILED, forcing new map lookup
(5) map lookup OK: map=mapfile[txt] key=prueba?prueba=1 -> val=/index.php
(4) RewriteCond: input='/index.php' pattern='>""' => matched
(5) cache lookup OK: map=mapfile[txt] key=prueba?prueba=1 -> val=/index.php
(2) rewrite '/prueba' -> '/index.php?'
Luis
  • 13
  • 3
  • Possible duplicate of http://stackoverflow.com/questions/10643705/mod-rewrite-query-string – Madbreaks Nov 14 '12 at 22:21
  • Thanks for the clue. old and new urls do not have correlation. New urls do not have querystring. What I want is test if old url(with querystring) is in file and map to new url. – Luis Nov 14 '12 at 22:30
  • @Luis I don't understand the problem with caching but if you want URI and query string in one variable, what about to extract it from `THE_REQUEST` variable? – Kamil Šrot Nov 15 '12 at 09:19
  • @KamilŠrot Thaks for your comment. I think it's more difficult with THE_REQUEST. %{THE_REQUEST} is the full HTTP request like 'GET /blablabla/index.php?args=1 HTTP/1.1'. – Luis Nov 15 '12 at 10:12
  • @Luis sure it is, but the regex wouldn't be too complicated. Anyway I can't help much as I don't follow your notes about caching performance. From my understanding, apache caches only the mapfile entries so you cannot affect the cache performance by a query composed from two variables – Kamil Šrot Nov 15 '12 at 10:20
  • @Luis Also have a look on `dbm` type of map file - will improve the performance dramatically – Kamil Šrot Nov 15 '12 at 10:27
  • Thank you @KamilŠrot. I mean that I always see in RewriteLog "cache lookup FAILED, forcing new map lookup". Don't know if this is because apache do not cache QUERY_STRING.... Im going to use dbm map file. Thanks again – Luis Nov 15 '12 at 11:03
  • @Luis can you post the rewrite log from two subsequent identical requests? Several lines above the notice of failed cache lookup should be enough... we need to make sure, the looked up key is exactly the same. The cache should indeed kick in. As per my research it also caches only successful lookups... – Kamil Šrot Nov 15 '12 at 11:13
  • Dont understand I see (6) cache lookup FAILED, forcing new map lookup and (5) cache lookup OK: map=mapfile[txt] key=prueba?prueba=1 -> val=/index.php [prueba?prueba=1 /index.php] – Luis Nov 15 '12 at 11:32

0 Answers0