0

可以说我想去

http://example.com/something/a/few/directories/deep

有没有一种方法可以在 PHP 中处理它而无需创建这些目录?我只想将域名之后的所有内容作为变量获取并从那里开始工作。

4

5 回答 5

2

Sure. You don't even need mod_rewrite if the beginning is constant or has only a few possible values. Make a php file named something (no extension) and tell Apache (via a SetHandler or similar directive) to treat it as PHP. Then, examine $_SERVER['PATH_INFO'] in the script, which will look like a/few/directories/deep IIRC. You can use that to route your request however you like.

Alternatively. as Martin says, use a simple mod_rewrite rule to rewrite it to /dispatch.php/something/a/few/directories/deep and then you have the whole path in PATH_INFO.

于 2009-10-27T23:34:09.023 回答
2

最常见的方法是使用 mod_rewrite。(在http://articles.sitepoint.com/article/guide-url-rewriting中有对此的介绍)。这允许您在内部将这些友好的 URL 映射到传递给 PHP 脚本的查询参数。

于 2009-10-27T23:31:21.103 回答
1

You do not need mod_rewrite to handle such a scenario. Most of the popular PHP frameworks currently support URI segment parsing. This is a simple example which still leaves a few security holes to be patched up.

You could handle this by taking one of the global $_SERVER variables and splitting it on forward slashes like so:

if (!empty($_SERVER['REQUEST_URI'])) {
    $segments = explode('/', $_SERVER['REQUEST_URI']);
}

The $segments variable will now contain an array of segments to iterate over.

于 2009-10-27T23:32:05.127 回答
1

假设您使用的是 apache ...

添加重写规则(在您的 vhost conf 中或只是将 .htaccess 文件放入您的 doc root 中),将所有内容指向 index.php。然后在您的 index.php 中,解析请求 uri 并提取路径

RewriteEngine on
RewriteRule ^.*$ index.php [L,QSA]

index.php:
$filepath = $_SERVER['REQUEST_URI'];
$filepath = ltrim($myname, '/'); //如果需要,去掉前导斜杠。

于 2009-10-27T23:45:36.853 回答
0

这实际上不是与 PHP5 相关的问题 - URL 重写是您的网络服务器的一部分。

当您在 Apache 上使用mod_rewrite时,您可以(不了解 IIS,但可能有类似的东西)。

于 2009-10-27T23:30:20.373 回答