如何在没有上下文路径的情况下获取 php 请求 URI?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10453248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get php request URI without context path?
提问by Brendan Coffey
$_SERVER['REQUEST_URI']returns URI with context path.
$_SERVER['REQUEST_URI']返回带有上下文路径的 URI。
For example, if the base URL of a website is http://localhost:8080/sitename/(i.e. the context path is sitename), and I use $_SERVER['REQUEST_URI']for http://localhost:8080/sitename/portfolio/design, it will return /sitename/portfolio/design.
例如,如果网站的基本 URL 是http://localhost:8080/sitename/(即上下文路径是站点名称),并且我使用$_SERVER['REQUEST_URI']for http://localhost:8080/sitename/portfolio/design,它将返回/sitename/portfolio/design.
I am then exploding the result to interpret my clean URLs:
然后我将分解结果以解释我的干净 URL:
$page=$_SERVER['REQUEST_URI'];
$segments=explode('/',trim($page,'/'));
$sitePage = $segments[1];//portfolio
This is working fine for my local testing environment, but on the production server, $segments[1]must become $segments[0]
这适用于我的本地测试环境,但在生产服务器上,$segments[1]必须变成$segments[0]
In order to use the same code in development and production, is there any way to get only this part /portfolio/design, i.e. the URI without context path?
为了在开发和生产中使用相同的代码,有没有办法只获取这部分/portfolio/design,即没有上下文路径的URI?
回答by jeroen
You can use virtual hosts so your test site will be on:
您可以使用虚拟主机,以便您的测试站点位于:
http://sitename.localhost:8080/
That way absolute paths (relative to the web-root) will be the same on the test server and the production server.
这样绝对路径(相对于 web-root)在测试服务器和生产服务器上将是相同的。
The how-to depends on the test server you are using.
操作方法取决于您使用的测试服务器。
回答by Lex
I solved this by comparing SCRIPT_NAMEand REQUEST_URIlike this:
我通过比较SCRIPT_NAME并REQUEST_URI像这样解决了这个问题:
// /sitename/index.php
$path = explode('/', trim($_SERVER['SCRIPT_NAME'], '/'));
// /sitename/portfolio/design
$uri = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
foreach ($path as $key => $val) {
if ($val == $uri[$key]) {
unset($uri[$key]);
} else {
break;
}
}
// portfolio/design
$uri = implode('/', $uri);
I'm guessing you have .htaccess routing to index.php
我猜你有 .htaccess 路由到 index.php
Hope that helps.
希望有帮助。
回答by Havelock
I would say that is the way to do what you wanted - get some part of the URI.
我会说这是做你想做的事情的方法 - 获取 URI 的一部分。
I would suggest you set up a virtual host (assuming you're using Apache), either Name or Address based. Then you could have something like http://127.0.0.42/portfolio/designor, with some editing of the hostfile on your system even http://sitename/portfolio/design. Then your developing environment will resemble the production one.
我建议您设置一个基于名称或地址的虚拟主机(假设您使用的是 Apache)。然后你可以有类似http://127.0.0.42/portfolio/design 的东西,或者在你的系统上对主机文件进行一些编辑,甚至是http://sitename/portfolio/design。那么您的开发环境将类似于生产环境。
Hope that helps.
希望有帮助。

