.htaccess - htaccess重写条件中的URL重写及变量解释

标签 .htaccess

我正在通过教程学习创建 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/

相关文章:

php - Codeigniter 3.1.6 - 如何从 url 中删除 index.php

regex - 带有编码新行的 mod_rewrite 正则表达式规则 (%0A)

php - 我可以将 .user.ini 文件放在 PHP 文档根目录之外吗?

.htaccess - 在某些 URL 上添加 SSL 可选

regex - .htaccess 正则表达式匹配最后一个斜杠之前的所有内容(甚至斜杠)

apache - mod_rewrite 已经启用但仍然得到 404

php - 为单个域强制使用 https(复杂的实现)

.htaccess - 重写规则 - a.php?a=123 到/b/123

apache - 将 RewriteCond 与 %{REQUEST_URI} 一起用于 mod_rewrite

wordpress - 如何将帖子重定向到 wordpress 中的类别帖子