通过 index.php 的所有请求,无一例外
ALL requests via index.php, no exceptions
这个问题我看过很多很好的答案,比如Pretty URLs in PHP frameworks,但是他们都明确排除了现有的文件和目录,现有的.php文件,index.php 文件本身 and/or .css/.js/etc。我想要 一切 定向到 index.php 文件,包括 index.php 文件,我可以在其中选择动态处理它的内容,例如压缩和缩小 css/js 文件或 404-ing 大多数真正存在的文件。
我一直在尝试很多事情来完成这个具有不同和奇怪的效果并寻找其他答案以寻求帮助,但无论我尝试什么方法总是有问题。我找到的最接近的工作方式也是最简单的...
RewriteRule .* index.php?__path__=[=11=] [L]
但是,问题是当我转到现有文件夹时没有输入尾随 / 例如localhost/test
它重定向到 localhost/test/
- 如果目录在服务器上不存在,它不会这样做。这在服务器上实际存在的事物和我试图摆脱的 URL 重写之间产生了非常明显的区别。
更奇怪的是,如果我转到 127.0.0.1/test
(如果某些 DNS 缓存怪癖导致此问题,这可能是一个修复),它会重定向到 http://127.0.0.1/test/?__path__=test
,这非常奇怪(尤其是自从to 127.0.0.1/test/
按预期完全避免了重定向,并且在重写中没有指定 [R]
),并揭示了我试图破坏的那种查询字符串 URL 。 /var=value
为查询字符串提供了更好的格式,但我离题了。当然,127.0.0.1/testa
不会发生重定向,因为该文件不存在,所以 Apache 似乎只是在故意做一些我真的不希望它做的事情。
另外,很奇怪,我找不到任何其他这样做的例子,这有什么大的缺点吗?我假设除了启动 PHP 和执行它必须执行的任何额外服务器负载之外,应该没有任何问题 - 哦,当然,错误可能会破坏对托管的所有内容的访问。我正在使用以下代码...
// trying to access a file directly?
if ($is_file) {
// TODO: manage served files (gzip, minify, etc.)
// is it an allowed extension?
if (!is_array($CONFIG['allow_ext_access']) || !in_array($type, $CONFIG['allow_ext_access']))
die('access denied');
// it's not *this* file is it?
if (!$is_index) {
// try to get known file type
$file_type = isset($CONFIG['file_types'][$type]) ? $CONFIG['file_types'][$type] : false;
// if we have a file type we can properly pass it as what it is
if ($file_type) {
header('Content-Type: '.$file_type['mime']);
}
// execute php file or just output any other file
if ($type == 'php') @include($path);
else @readfile($path);
die;
}
}
// if it's not this file, then it's a path and a URL for re-routing
如果我没看错你可以使用:
DirectorySlash Off
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule .* index.php?__path__=[=10=] [L,QSA]
DirectorySlash Off
用于禁用在真实目录后添加尾部斜杠,因为我们将所有内容(包括现有文件/目录)路由到 index.php
.
确保在清除浏览器缓存后对其进行测试。
我们在这里使用 ENV:REDIRECT_STATUS
变量,这是一个内部 mod_rewrite
变量,在内部重写成功后设置为 200
。通过检查
RewriteCond %{ENV:REDIRECT_STATUS} ^$
我们确保只进行第一次重写并且没有循环。
这个问题我看过很多很好的答案,比如Pretty URLs in PHP frameworks,但是他们都明确排除了现有的文件和目录,现有的.php文件,index.php 文件本身 and/or .css/.js/etc。我想要 一切 定向到 index.php 文件,包括 index.php 文件,我可以在其中选择动态处理它的内容,例如压缩和缩小 css/js 文件或 404-ing 大多数真正存在的文件。
我一直在尝试很多事情来完成这个具有不同和奇怪的效果并寻找其他答案以寻求帮助,但无论我尝试什么方法总是有问题。我找到的最接近的工作方式也是最简单的...
RewriteRule .* index.php?__path__=[=11=] [L]
但是,问题是当我转到现有文件夹时没有输入尾随 / 例如localhost/test
它重定向到 localhost/test/
- 如果目录在服务器上不存在,它不会这样做。这在服务器上实际存在的事物和我试图摆脱的 URL 重写之间产生了非常明显的区别。
更奇怪的是,如果我转到 127.0.0.1/test
(如果某些 DNS 缓存怪癖导致此问题,这可能是一个修复),它会重定向到 http://127.0.0.1/test/?__path__=test
,这非常奇怪(尤其是自从to 127.0.0.1/test/
按预期完全避免了重定向,并且在重写中没有指定 [R]
),并揭示了我试图破坏的那种查询字符串 URL 。 /var=value
为查询字符串提供了更好的格式,但我离题了。当然,127.0.0.1/testa
不会发生重定向,因为该文件不存在,所以 Apache 似乎只是在故意做一些我真的不希望它做的事情。
另外,很奇怪,我找不到任何其他这样做的例子,这有什么大的缺点吗?我假设除了启动 PHP 和执行它必须执行的任何额外服务器负载之外,应该没有任何问题 - 哦,当然,错误可能会破坏对托管的所有内容的访问。我正在使用以下代码...
// trying to access a file directly?
if ($is_file) {
// TODO: manage served files (gzip, minify, etc.)
// is it an allowed extension?
if (!is_array($CONFIG['allow_ext_access']) || !in_array($type, $CONFIG['allow_ext_access']))
die('access denied');
// it's not *this* file is it?
if (!$is_index) {
// try to get known file type
$file_type = isset($CONFIG['file_types'][$type]) ? $CONFIG['file_types'][$type] : false;
// if we have a file type we can properly pass it as what it is
if ($file_type) {
header('Content-Type: '.$file_type['mime']);
}
// execute php file or just output any other file
if ($type == 'php') @include($path);
else @readfile($path);
die;
}
}
// if it's not this file, then it's a path and a URL for re-routing
如果我没看错你可以使用:
DirectorySlash Off
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule .* index.php?__path__=[=10=] [L,QSA]
DirectorySlash Off
用于禁用在真实目录后添加尾部斜杠,因为我们将所有内容(包括现有文件/目录)路由到 index.php
.
确保在清除浏览器缓存后对其进行测试。
我们在这里使用 ENV:REDIRECT_STATUS
变量,这是一个内部 mod_rewrite
变量,在内部重写成功后设置为 200
。通过检查
RewriteCond %{ENV:REDIRECT_STATUS} ^$
我们确保只进行第一次重写并且没有循环。