在不使用 .htaccess 的情况下开发网站导航时,我有哪些替代方案?

发布于 2024-08-15 03:39:04 字数 374 浏览 1 评论 0原文

理论上:几乎所有现代网站都以这种方式工作:

somedomain.com/layer1/layer2/action

例如:

http://stackoverflow.com/questions/ask

通常这是使用 .htaccess 和 ModRewrite 引擎完成的。不久前,我偶然发现一篇文章,其中描述了一些直接来自 php 的东西。我不记得这个名字了。那段 PHP 代码知道当前打开的 URL。它能够告诉什么域名被调用,什么TLD被调用,第一个目录是什么,第二个目录是什么,被调用的文件是什么,等等。

我知道Kohana框架使用这种技术,但不知道在哪里看。有人熟悉这个吗?

Just in theory: Almost all modern websites work this way:

somedomain.com/layer1/layer2/action

For example:

http://stackoverflow.com/questions/ask

Typically this is done with .htaccess and ModRewrite Engine. A while ago I stumbled over an article that described something which came right out of php. I don't remember the name anymore. That piece of PHP code was aware of the currently opened URL. It was able to tell what domain is called, what TLD is called, what's the first directory, what's the second directory, what's the called file, etc.

I know the Kohana Framework uses this technology, but don't know where to look at. Anyone familiar with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青丝拂面 2024-08-22 03:39:04

您的意思是利用 PATH_INFO 环境变量。
请参阅此处

无法访问 .htaccess 设置,但是,使用此方法,路径中始终会有一个 filename.php

www.domain.com/filename.php/questions/ask/

What you mean is making use of the PATH_INFO environment variable.
See e.g. here

Without access to .htaccess settings, you will however always have one filename.php in the path with this method:

www.domain.com/filename.php/questions/ask/
沒落の蓅哖 2024-08-22 03:39:04

在大多数情况下,.htaccess 表面下发生的事情正是您要寻找的。许多情况下,.htaccess 文件只是将任务委托给 index.php 文件,该文件本身准备好处理传入的表示控制器、方法和参数的值。

我不明白为什么您不能简单地创建自己的 .php 文件来解析 url 并处理其中的值。不过,您只需使用该 .php 文件作为所有网址的基础即可:

www.mysite.com/index.php/controller/method/parameter1/parameter2

What is happening beneath the surface of .htaccess in most cases is what you're looking for. Many cases the .htaccess file simply delegates the task to an index.php file, which itself is prepared to handle the values passed in representing a controller, a method, and parameters.

I don't see why you couldn't simply create your own .php file to parse out the url and handle the values from there. You'll merely have to use that .php file as your base in all urls though:

www.mysite.com/index.php/controller/method/parameter1/parameter2
涫野音 2024-08-22 03:39:04

您可以使用 apache 的错误文档,但这只是一个 hack(以及重写)而不是一个干净的解决方案。

You could play with apache's error document, but it's just a hack (as the rewrite as well) and not a clean solution.

星軌x 2024-08-22 03:39:04

您可以使用 HTTP 标头的组合,但这些标头在某些 Web 服务器上可能不可靠。要查看服务器上可用的 HTTP 标头,请创建一个包含以下内容的页面:

<?php phpinfo(); ?>

查找 PHP 变量部分,具体来说,这些是您可以使用的变量:

_SERVER["HTTP_HOST"]
_SERVER["SCRIPT_NAME"]

You could use a combination of HTTP headers, but those can be unreliable on some web servers. To see what HTTP headers are available on your server, make a page with just this:

<?php phpinfo(); ?>

Look for the PHP Variables section, specifically these are the ones that you can potentially use:

_SERVER["HTTP_HOST"]
_SERVER["SCRIPT_NAME"]
妳是的陽光 2024-08-22 03:39:04

查看前端控制器模式

本质上,您可以将所有请求重定向到单个 PHP 文件,该文件对 URL 进行解码并决定下一步操作。

顺便说一句,这不是特定于 PHP 的。

Have a look at the Front controller pattern.

Essentially you get all your requests to be redirected to a single PHP file that decodes the URL and decides on the next action.

This isn't PHP-specific btw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文