场景:我需要能够重定向某些 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 forbidden。
为什么会发生 403 Forbidden,从日志中可以看出,当请求页面但不在 RewriteMap examplemap (rewrite.dbm) 中时,它会发送/
。我以为会是一个缺失的索引,但是添加-Indexes
到Options
没有任何变化,并且/
确实有一个在 vhost 中定义的 index.htm。
问题是,当发出请求时,如果 URL 不在 RewriteMap examplemap (rewrite.dbm) 文件中,我怎样才能不重写?
或者是否有更好的方法来混合重写和重定向,记住这仅适用于生产中的 POC,我将有几百个重写或重定向,并且 dbm 文件将在新页面移动时更新。
日志输出LogLevel alert rewrite:trace3
示例.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>
答案1
我认为触发 403 是因为您的DirectoryIndex
文档(即index.htm
)不存在。然而,这只是真正的潜在问题的最终结果……您无条件地重写你的返回值示例图 RewriteMap
(可能为“空”)。我怀疑如果你example.com/test1/ucm12347.htm
直接请求或任何你不存在的 URL,你会看到相同的结果。示例图 RewriteMap
。
当在您的示例图 RewriteMap
(每个没有重定向),则最终会重写到/
(文档根目录),因为重写映射返回的结果为空。这将触发 mod_dir 为 提供服务DirectoryIndex
。如果DirectoryIndex
文档不存在,并且Indexes
选项未启用,那么您将收到 403 Forbidden。
你用你的解决了这个问题重定向 RewriteMap
通过创建一个状况首先检查 的返回值RewriteMap
。您需要对您的示例图 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]