如何仅在 PHP 中重写 URL? [英] How To Rewrite URL in PHP Only?

查看:42
本文介绍了如何仅在 PHP 中重写 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑一个 MVC(模型、视图、控制器)结构化站点.它托管在 smallbusiness.yahoo.com 上.(Apache) 他们将托管限制为 php(和 mysql),我无法编辑服务器配置.这意味着没有 .htaccess 文件!找了很久的解决方案,都涉及到编辑服务器配置.我正在寻找一种仅用 php 本身重写 URL 的方法.

I have a MVC (Model, View, Controller) structured site that I'm thinking about. It's hosted on smallbusiness.yahoo.com. (Apache) They limit the hosting to php (and mysql), and there's no way I can edit the server configurations. That means no .htaccess files! I have searched for the solution for a long time, but they all involve editing the server configurations. I am looking for a way to rewrite URLs with only php itself.

我想做 URL 重写,以便控制器可以通过 URL 接收参数.我知道 Codeigniter 会这样做,但我确实想了解它是如何做到的.

I want to do URL rewriting so that the controller(s) can receive parameters through the URL. I know Codeigniter does this, but I do want to LEARN how it does it.

例如,像这样:

example.com/page.php?page=whatever

example.com/page.php?page=whatever

变成

example.com/page/whatever 或

example.com/page/whatever OR

example.com/whatever

example.com/whatever

我看过一些关于 $_SERVER['PATH_INFO'] 的例子,但我不明白它的作用.我试过了

I've seen some examples with $_SERVER['PATH_INFO'], but I don't understand what that does. I've tried

$_SERVER['PATH_INFO'] = '/';

但这并没有起到任何作用......将不胜感激!

But that did nothing... Help would be greatly appreciated!!

推荐答案

这是我能找到的最接近的东西.严格来说,即使是 WordPress 也会修改 .htaccess 以允许他们自己的模块来管理重写.我觉得奇怪的是,他们会完全禁止所有 .htaccess 更改.你试过了吗?甚至 ASP.NET 也需要修改 web.config 才能使用重写.

This is the closest thing I could find. Strictly speaking, even WordPress modifies the .htaccess to allow their own module to manage the rewriting. I find it odd that they would completely disallow all .htaccess changes. Have you given it a try? Even ASP.NET requires a web.config modification to use rewrites.

http://pure-essence.net/2007/06/29/simple-php-path-rewrite/

显然,如果 .htaccess 被完全禁止,最后一个解决方案将无济于事,但在您实际使用 PHP 的情况下,这种情况很少见.我可以看到他们不允许覆盖,但这通常只适用于索引和目录选项之类的东西.

Obviously the last solution wouldn't help if .htaccess is disallowed entirely, but that's rarely the case where you can actually use PHP. I can see them disallowing overrides, but that usually only applies to things like indexes and directory options.

这篇关于如何仅在 PHP 中重写 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆