【问题标题】:Reduce redirects with RewriteMap使用 RewriteMap 减少重定向
【发布时间】:2020-01-14 01:03:02
【问题描述】:

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

配置:

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

RewriteMap test "txt:/tmp/redirects.txt"  #/old   /new
RedirectMatch ^/(.*?)/$ /$1
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

【问题讨论】:

    标签: apache redirect http-status-code-301 http-redirect rewritemap


    【解决方案1】:

    试试这个:

    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 ^/(.*?)/$ /$1
    

    【讨论】:

      猜你喜欢
      • 2018-12-15
      • 2010-12-25
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多