使用 RewriteMap 减少重定向

Reduce redirects with RewriteMap

我在一个文件中有一个 RewriteMap,并且想要处理(忽略)可选的尾随斜杠。我想尽量减少尾部斜杠案例中的重定向次数。如何更改 apache-config?

配置:

<VirtualHost *:80>
ServerName redirtest.localhost
RewriteEngine on

RewriteMap test "txt:/tmp/redirects.txt"  #/old   /new
RedirectMatch ^/(.*?)/$ /
RewriteCond %{REQUEST_URI}           ^(.*)$
RewriteCond ${test:%1|NOT_PRESENT}  !NOT_PRESENT [NC]
RewriteRule .?                       ${test:%1} [NC,R=301]

结果(正常):

curl -sIL 'http://redirtest.localhost/old?key=val&somemore=foobar' |grep Location 
Location: http://redirtest.localhost/new?key=val&somemore=foobar

以下有效,但我想避免中间重定向:

curl -sIL 'http://redirtest.localhost/old/?key=val&somemore=foobar' |grep Location
Location: http://redirtest.localhost/old?key=val&somemore=foobar
Location: http://redirtest.localhost/new?key=val&somemore=foobar

试试这个:

RewriteMap test "txt:/tmp/redirects.txt" 
RewriteCond %{REQUEST_URI}           ^(.*\w)/?$
RewriteCond ${test:%1|NOT_PRESENT}  !NOT_PRESENT [NC]
RewriteRule .?                       ${test:%1} [NC,R=301]
RedirectMatch ^/(.*?)/$ /