我正在通过教程学习创建 REST API。 在本教程中,有一个步骤执行 URL 重写。代码如下:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
谁能解释一下重写条件和其中的 %{ENV:BASE}
部分?
最佳答案
条件很简单:
如果请求的 URI 未指示任何现有文件,则重定向到 ${ENV:BASE}index.php
.
您可以找到 RewriteRule 标志的描述 here .简而言之:
QSA
代表“查询字符串追加”。这意味着查询字符串将附加到替换 URI。
L
表示这是最后一条规则,不会处理其他规则。
关于 this page htaccess 中对环境变量有很好的解释:
The %{ENV:variable} form of TestString in the RewriteCond allows mod_rewrite's rewrite engine to make decisions conditional on environment variables. Note that the variables accessible in mod_rewrite without the ENV: prefix are not actually environment variables. Rather, they are variables special to mod_rewrite which cannot be accessed from other modules.
因此,您可以像这样在规则中使用环境变量 ${ENV:<name>}
.当然,变量必须提前设置。
关于.htaccess - htaccess重写条件中的URL重写及变量解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173706/