apache-2.4 – 如果url不在列表中,则混合DBM重写并重定向如何停止重写

场景:我需要能够重定向某些网址并重写其他网址

现在经过多次修修补补后,我几乎可以完成所有工作

如果我点击example.com正常工作和200

如果我点击example.com/testrea,则会显示example.com/test11/ucm25685.htm中的文本和200

如果我点击example.com/retest111/totest11.htm,重定向到example.com/test1/ucm12347.htm确实会发生,但我得到403 Forbidden

我怎么停止403 Forbidden?我想我知道它为什么会发生,但无法弄清楚如何阻止它.如果我禁用重写部分,重定向工作查找并且不显示403禁止.

为什么403 Forbidden发生,正如日志所见,当请求页面而不是RewriteMap examplemap(rewrite.dbm)时它将发送/.其中我会丢失索引,但是添加-Indexes对Options没有做任何更改,并且/确实有一个在vhost中定义的index.htm.

问题是当请求发出时如果url不在RewriteMap examplemap(rewrite.dbm)文件中,我怎么能不重写?

或者有更好的方法来混合重写和重定向,记住这只适用于生产中的POC我将有几百个重写或重定向,并且dbm文件将在移动新页面时更新.

日志输出LogLevel警告重写:trace3

example.com

[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56079] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f697290/initial] init rewrite engine with requested uri /
[DATE] [rewrite:trace3] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56079] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f697290/initial] applying pattern '^(.*)' to uri '/'
[DATE] [rewrite:trace3] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56079] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f697290/initial] applying pattern '^(.*)' to uri '/'
[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56079] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f697290/initial] rewrite '/' -> '/index.htm'
[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56079] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f697290/initial] local path result: /index.htm
[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56079] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f697290/initial] prefixed with document_root to /var/www/sites/me/index.htm
[DATE] [rewrite:trace1] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56079] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f697290/initial] go-ahead with /var/www/sites/me/index.htm [OK]

example.com/testrea

[DATE] [rewrite:trace2] [pid 10407] mod_rewrite.c(470): [client 55.66.77.88:56111] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f698700/initial] init rewrite engine with requested uri /testrea
[DATE] [rewrite:trace3] [pid 10407] mod_rewrite.c(470): [client 55.66.77.88:56111] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f698700/initial] applying pattern '^(.*)' to uri '/testrea'
[DATE] [rewrite:trace3] [pid 10407] mod_rewrite.c(470): [client 55.66.77.88:56111] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f698700/initial] applying pattern '^(.*)' to uri '/testrea'
[DATE] [rewrite:trace2] [pid 10407] mod_rewrite.c(470): [client 55.66.77.88:56111] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f698700/initial] rewrite '/testrea' -> '/test11/ucm25685.htm'
[DATE] [rewrite:trace2] [pid 10407] mod_rewrite.c(470): [client 55.66.77.88:56111] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f698700/initial] local path result: /test11/ucm25685.htm
[DATE] [rewrite:trace2] [pid 10407] mod_rewrite.c(470): [client 55.66.77.88:56111] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f698700/initial] prefixed with document_root to /var/www/sites/me/test11/ucm25685.htm
[DATE] [rewrite:trace1] [pid 10407] mod_rewrite.c(470): [client 55.66.77.88:56111] 55.66.77.88 - - [example.com/sid#55ac8f2ead48][rid#55ac8f698700/initial] go-ahead with /var/www/sites/me/test11/ucm25685.htm [OK]

example.com/retest111/totest11.htm

[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f69f2d0/initial] init rewrite engine with requested uri /retest111/totest11.htm
[DATE] [rewrite:trace3] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f69f2d0/initial] applying pattern '^(.*)' to uri '/retest111/totest11.htm'
[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f69f2d0/initial] rewrite '/retest111/totest11.htm' -> '/test1/ucm12347.htm'
[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f69f2d0/initial] explicitly forcing redirect with http://example.com/test1/ucm12347.htm
[DATE] [rewrite:trace1] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f69f2d0/initial] escaping http://example.com/test1/ucm12347.htm for redirect
[DATE] [rewrite:trace1] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f69f2d0/initial] redirect to http://example.com/test1/ucm12347.htm [REDIRECT/301]
[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f691260/initial] init rewrite engine with requested uri /test1/ucm12347.htm
[DATE] [rewrite:trace3] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f691260/initial] applying pattern '^(.*)' to uri '/test1/ucm12347.htm'
[DATE] [rewrite:trace3] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f691260/initial] applying pattern '^(.*)' to uri '/test1/ucm12347.htm'
[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f691260/initial] rewrite '/test1/ucm12347.htm' -> '/'
[DATE] [rewrite:trace2] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f691260/initial] local path result: /
[DATE] [rewrite:trace1] [pid 10412] mod_rewrite.c(470): [client 55.66.77.88:56126] 55.66.77.88  - - [example.com/sid#55ac8f2ead48][rid#55ac8f691260/initial] go-ahead with / [OK]

Webroot的当前布局

test1/ucm12347.htm
test2/ucm23452.htm
test3/ucm34674.htm
test4/ucm43489.htm
test5/ucm10963.htm

test11/ucm25685.htm
test22/ucm87546.htm
test33/ucm32548.htm
test44/ucm95142.htm
test55/ucm56485.htm

/retest111/totest11.htm
/retest222/totest22.htm
/retest333/totest33.htm
/retest444/totest44.htm
/retest555/totest55.htm

2正在转换为DB和DBM文件的文本文件:

rewrite.txt

httxt2dbm -i rewrite.txt -o rewrite.dbm

/ index.htm
/testrea test11/ucm25685.htm
/testreb test22/ucm87546.htm
/testrec test33/ucm32548.htm
/testred test44/ucm95142.htm
/testree test55/ucm56485.htm

redirects.txt

httxt2dbm -f db -i redirects.txt -o redirects.db

/retest111/totest11.htm test1/ucm12347.htm
/retest222/totest22.htm test2/ucm23452.htm
/retest333/totest33.htm test3/ucm34674.htm
/retest444/totest44.htm test4/ucm43489.htm
/retest555/totest55.htm test5/ucm10963.htm

在httpd.conf中我有一个全局重写

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteMap examplemap "dbm:/etc/httpd/conf/rewrite.dbm"
    RewriteRule ^(.*) /${examplemap:$1}
</IfModule>

还有一个单独的虚拟主机

<VirtualHost *:80>
    DocumentRoot /var/www/sites/me
    ServerName example.com
    DirectoryIndex index.htm
    ErrorDocument 404 /index.php
    Options +FollowSymLinks
    RewriteEngine On
    RewriteOptions Inherit
    RewriteMap redirects dbm=db:/etc/httpd/conf/redirects.db
    RewriteCond ${redirects:$1} !=""
    RewriteRule ^(.*) /${redirects:$1} [redirect=permanent,last]
</VirtualHost>
最佳答案
我认为403是被触发的,因为您的DirectoryIndex文档(即index.htm)不存在.然而,这只是真正潜在问题的最终结果……你无条件地重写你的examplemap RewriteMap的返回值(可能是“空”).如果您直接请求example.com/test1/ucm12347.htm或您的examplemap RewriteMap中不存在的任何URL,我怀疑您会看到相同的结果.

如果在您的examplemap RewriteMap中找不到查找(在每个不重定向的请求上都会查找),那么您最终会重写为/(文档根目录),因为从重写映射返回的结果为空.这将触发mod_dir以提供DirectoryIndex.如果DirectoryIndex文档不存在且未启用“索引”选项,那么您将获得403 Forbidden.

您通过创建首先检查RewriteMap返回值的条件,重定向RewriteMap解决了这个问题.你需要用你的examplemap RewriteMap做类似的事情.例如:

RewriteMap examplemap "dbm:/etc/httpd/conf/rewrite.dbm"
RewriteCond ${examplemap:$1} !=""
RewriteRule ^(.*) /${examplemap:$1} [L]

替代方案:

06001

您可以将RewriteMap查找的结果存储在环境变量中,以避免多次查找(在条件和RewriteRule替换中).虽然RewriteMap查找的结果是缓存的,但这可能看不到任何好处.例如:

RewriteRule (.*) - [E=REDIRECT_TO:${redirects:$1}]
RewriteCond %{ENV:REDIRECT_TO} .
RewriteRule ^ /%{ENV:REDIRECT_TO} [redirect=permanent,last]

转载注明原文:apache-2.4 – 如果url不在列表中,则混合DBM重写并重定向如何停止重写 - 代码日志