gpt4 book ai didi

php - 将所有请求重定向到 index.php 而没有 .htaccess

转载 作者:行者123 更新时间:2023-12-04 21:24:08 24 4
gpt4 key购买 nike

我目前正在使用以下命令将所有请求重定向到 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>

虽然这有效,但它限制了我的代码只能在启用了 mod_rewrite 的服务器上运行。所以我想知道,有没有办法在不使用 .htaccess 的情况下模拟这种行为?其他框架是如何做到的?

最佳答案

像 Yahoo 这样的主机不允许 .htaccess,这可能有点令人沮丧。据我所知,这样重定向是不可能的,但这是一种解决方法:

有这个文件结构:

index.php
private/ <-- you could do: password protect, set permissions, name with hard to guess string, etc
public/

所以访问 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));

(可选)删除空参数: array_filter($uri_args);
看我的问题: How To Rewrite URL in PHP Only?
(另见 http://php.net/manual/en/reserved.variables.server.phpRemove empty array elements)

关于php - 将所有请求重定向到 index.php 而没有 .htaccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21801778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com