1

我需要将(301)数百个旧的非结构化 url 重定向到新的。旧网址有查询字符串 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 中包含查询字符串?

我这样解决了:

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

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

重写日志

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

0 回答 0