Apache-2.4

如果 url 不在列表中,混合 DBM 重寫和重定向如何停止重寫

  • September 24, 2018

場景:我需要能夠重定向某些 url 並重寫其他 URL

現在,經過多次修補,我幾乎可以讓所有東西正常工作

如果我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) 中時,它會發送/. 我雖然會是一個缺失的索引,但是添加-IndexesOptions沒有更改,並且/確實有一個在 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 文件的文本文件:

重寫.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

重定向.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)不存在。但是,這只是真正潛在問題的最終結果……您正在無條件地重寫您的範例映射 的返回值RewriteMap(可能是“空的”)。我懷疑如果您直接請求或您的examplemapexample.com/test1/ucm12347.htm中不存在任何 URL,您會看到相同的結果。 RewriteMap

當在您的examplemap RewriteMap中找不到查找(在每個未重定向的請求上查找)時,您最終會重寫到/(文件根),因為從您的重寫映射返回的結果是空的。這將觸發 mod_dir 為DirectoryIndex. 如果DirectoryIndex文件不存在並且該Indexes 選項未啟用,那麼您將收到 403 Forbidden。

通過 RewriteMap創建一個首先檢查來自RewriteMap. 您需要對您的examplemap RewriteMap做類似的事情。例如:

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

選擇:

RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*) /${redirects:$1} [redirect=permanent,last]

您可以將RewriteMap查找結果儲存在環境變數中,以避免多次查找(在條件RewriteRule 替換中)。雖然RewriteMap查找的結果被記憶體了,所以這可能看不到任何好處。例如:

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

引用自:https://serverfault.com/questions/931922