Apache rewrite 部分细节 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cloudL
V2EX    Apache

Apache rewrite 部分细节

  •  
  •   cloudL 2017-02-15 15:48:48 +08:00 2255 次点击
    这是一个创建于 3207 天前的主题,其中的信息可能已经有所发展或是发生改变。

    多个.htaccess 匹配的优先级

    网站根路径下有一个.htaccess ,根目录下的文件夹 test 中也有一个.htaccess ,那么,访问 http://localhost/test/xyz 会有什么结果呢( test 下不存在名为 xyz 的文件),或者说如果两个.htaccess 都会对错误的访问进行处理,谁的优先级更高?

    根路径下:

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} s [OR]

    RewriteCond %{REQUEST_FILENAME} -l [OR]

    RewriteCond %{REQUEST_FILENAME} -d

    RewriteRule ^.*$ - [L]

    RewriteRule ^.*$ https://baidu.com [R=301,L]

    test 路径下:

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -s [OR]

    RewriteCond %{REQUEST_FILENAME} -l [OR]

    RewriteCond %{REQUEST_FILENAME} -d

    RewriteRule ^.*$ - [L]

    RewriteRule ^.*$ https://bing.com [R=301,L]

    测试结果是 test 路径下的.htaccess 起效了,也就是在浏览器上访问 http://localhost/test/xyz 实际上跳转到了 Bing 。当我把 test 下的.htaccess 移除后则会跳转到 Baidu.

    罗列一些细节

    $N 匹配的是 RewriteRule 中正则匹配的内容(RewriteRule backreferences),而%N 匹配的是 RewriteCond 中正则匹配的内容(RewriteCond backreferences),而\N 则是正则表达式中原生的反向引用 有时候出现的::并不是特殊的符号,只是方便使用的分隔符而已,喜欢的话可以用##等替代 12 RewriteCond TestString CondPattern 中, TestString 可以是以下内容构成的:

    RewriteRule backreferences

    RewriteCond backreferences

    RewriteMap expansions 3

    Server-Variables 4

    RewriteRule Pattern Substitution [flags]中, pattern 匹配的是什么?就我的理解来说,如果访问的链接是 http://localhost:80/test/hello/hahahaha?x=100 ,

    如果它由网站根目录下的.htaccess 来处理,它匹配的内容就是 test/hello/hahahaha; 如果它由根目录 /test/.htaccess 来处理,它匹配的内容就是 hello/hahahaha

    What is matched?5 In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. “/app1/index.html ”). In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (e.g. “ app1/index.html ” or “ index.html ” depending on where the directives are defined). If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively. In any case, remember that regular expressions are substring matches. That is, you don ’ t need the regex to describe the entire string, just the part that you wish to match. Thus, using a regex of . is often sufficient rather than .*, and the regex abc is not the same as ^abc$.

    以后再补充。。。。。

    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5276 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 01:27 PVG 09:27 LAX 17:27 JFK 20:27
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86