了解.htaccess重写规则

问题描述 投票:0回答:1

我偶然发现了这个.htaccess文件,并试图找出它的作用。

RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !upload\.php$
RewriteRule ^ - [L,H=default-handler]
.htaccess
1个回答
0
投票
RewriteEngine On

上面的行说,你激活mod_rewrite

RewriteCond %{REQUEST_URI} !/$

上面的行是一个应用URI的条件,URI是主机不包括查询字符串http://httpd.apache.org/docs/current/mod/mod_rewrite.html后的路径的一部分,例如http://example.com/whatever/index.php,/ whatever / index.php是本例中的URI部分,然后有!/ $ with意思是!不结束$ with / so这个条件将匹配任何不以/结尾的URI

RewriteCond %{REQUEST_URI} !index\.php$
RewriteCond %{REQUEST_URI} !upload\.php$

上面的两行也是不匹配以index.php或upload.php结尾的URI的条件

RewriteRule ^ - [L,H=default-handler]

上面的行说明了应该根据这些条件应用的规则,它包括模式,替换和标志,你可以在这里阅读更多https://httpd.apache.org/docs/2.4/rewrite/intro.html,这里意味着任何请求传递上面的条件将由默认处理程序处理,所以模式是^这是常规的表达式https://httpd.apache.org/docs/2.4/rewrite/intro.html表示匹配字符串的开头,替换是 - 这意味着什么都不做,然后标志[L,H =默认处理程序],L表示停止处理规则集,H表示强制使用指定处理结果请求处理程序,它是你的情况下的默认处理程序。

看到这个关于标志https://httpd.apache.org/docs/2.4/rewrite/flags.html这和理解处理程序https://httpd.apache.org/docs/2.4/handler.html

© www.soinside.com 2019 - 2024. All rights reserved.