mod_rewrite 301重定向数百个旧的非结构化URL

时间:2012-11-14 22:19:53

标签: mod-rewrite redirect apache2 rewritemap

我需要将数百个旧的非结构化网址重定向(301)到新的网址。旧网址有querystring domain / directories / randomtext?args = 1

我有一个带有oldurl newurl行的文件。

我正在尝试使用虚拟主机中的RewriteMap

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

是否可以在RewriteCond中包含Querystring?

我这样解决了:

#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]

我认为这可以改进,因为缓存查找总是失败。我想只有cache_uri被缓存了。

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?'

0 个答案:

没有答案