How To Rewrite URL in PHP Only?

2019-08-27 23:37发布

问题:

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.

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.

For example, something like this:

example.com/page.php?page=whatever

turns into

example.com/page/whatever OR

example.com/whatever

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!!

回答1:

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/

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.



回答2:

I advise you to take a look at this, maybe it could help you.