如何将域名后的所有内容都变成字符串 [英] How to get everything after the domain name into a string

查看:25
本文介绍了如何将域名后的所有内容都变成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将域名后的所有内容都变成一个字符串.就像在 mysite.com/page/page2 中一样,会产生一个字符串page/page2".我可以做到,但是它开始给我带来问题,例如,当站点位于子文件夹中而不是根目录中时,站点所在的文件夹也将包含在字符串中,如果我不使用 mod_rewrite获得漂亮的链接,它还会将 index.php 添加到字符串中.

因此,我需要一两个技巧来让脚本了解该站点是否位于诸如 mysite.com/sitefolder/page/page2 之类的子文件夹中,并且它仍然会产生一个字符串

page/page2

如果站点没有使用mod_rewrite并且url是mysite.com/sitefolder/index.php/page/page2,它仍然会产生一个字符串

page/page2

请记住,我在配置文件中定义了 URL 和 USE_MOD_REWRITE,所以不需要魔法.我只是不知道如何获取该字符串.我知道我可以执行 $_SERVER['REQUEST_URI'] 来获取字符串,但是 index.php 仍然会在其中.如果我解释得不够好,我很抱歉,但感谢所有帮助.

解决方案

  • 解析请求uri获取路径
  • 从字符串的末尾删除最终的脚本名称(例如 index.php)
  • 删除任何尾部斜杠

    <前>$request = parse_url($_SERVER['REQUEST_URI']);$path = $request["path"];$result = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');

编辑

$request = parse_url($_SERVER['REQUEST_URI']);$path = $request["path"];$result = trim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');$result =explode('/', $result);$max_level = 2;而 ($max_level < count($result)) {未设置($result[0]);}$result = '/'.implode('/', $result);

My goal is to get everything after the domain name into a string. As in mysite.com/page/page2 would result in a string "page/page2". That I can do, however it starts giving me problems when, for example, the site is in a subfolder and not in root, then the folder the site is in will also be included in the string and if I'm not using mod_rewrite to get pretty links, it will also add index.php to the string.

So, I would need a trick or two to make the script understand whether or not the site is in a subfolder such as mysite.com/sitefolder/page/page2 and that it would still result in a string

page/page2

If the site doesn not use mod_rewrite and the url is mysite.com/sitefolder/index.php/page/page2, it would still result in a string

page/page2

Keep in mind that I have URL and USE_MOD_REWRITE defined in a config file, so no need for magic. I just have no idea how to go about getting that string. I know I could do $_SERVER['REQUEST_URI'] to get the string, but then index.php would still be in it. I'm sorry if I didn't explain well enough, but all help is appreciated.

解决方案

  • get the path by parsing the request uri
  • remove eventual script name from the end of the string (e.g. index.php)
  • rtrim any trailing slashes

    $request = parse_url($_SERVER['REQUEST_URI']);
    $path = $request["path"];
    $result = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');
    

EDIT

$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];

$result = trim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $path), '/');

$result = explode('/', $result);
$max_level = 2;
while ($max_level < count($result)) {
    unset($result[0]);
}
$result = '/'.implode('/', $result);

这篇关于如何将域名后的所有内容都变成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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