我目前使用以下命令将所有请求重定向到 index.php 文件:
<IfModule mod_rewrite.c>
RewriteEngine on
# base path, if not the root directory
RewriteBase /dev/public/
# direct all requests to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
</IfModule>
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但它限制我的代码只能在启用了 mod_rewrite 的服务器上运行。所以我想知道,有没有办法在不使用 .htaccess 的情况下模拟这种行为?其他框架是如何做到的?
小智 1
像雅虎这样的主机不允许 .htaccess,这可能有点令人沮丧。据我所知,这样的重定向是不可能的,但这是一种解决方法:
有这个文件结构:
index.php
private/ <-- you could do: password protect, set permissions, name with hard to guess string, etc
public/
Run Code Online (Sandbox Code Playgroud)
因此访问example.com/page将输出 404,但您可以将 url 重做为example.com/index.php/page
这是我用来获取数组中的 URI 参数的方法:
$uri_args =
explode('/', substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'index.php') + 10));
Run Code Online (Sandbox Code Playgroud)
(可选)删除空参数:
array_filter($uri_args);
请参阅我的问题:如何仅在 PHP 中重写 URL?
(另请参阅http://php.net/manual/en/reserved.variables.server.php和删除空数组元素)