php中将绝对路径转换为相对路径

发布于 2024-10-02 07:04:36 字数 733 浏览 2 评论 0原文

我想将绝对路径转换为相对路径。

这就是当前绝对代码的样子

$sitefolder  = "/wmt/";
$adminfolder = "/wmt/admin/";

$site_path = $_SERVER["DOCUMENT_ROOT"]."$sitefolder";

// $site_path ="//winam/refiller/";

$admin_path = $site_path . "$adminfolder";

$site_url    = "http://".$_SERVER["HTTP_HOST"]."$sitefolder";
$admin_url   = $site_url . "$adminfolder";

$site_images = $site_url."images/"; 

,例如,上面的代码将为您提供一个站点 url

www.temiremi.com/wmt 

并访问其中的文件,这将给出

www.temiremi.com/wmt/folder1.php

我想要做的是我想屏蔽 temiremi.com/ wmt 并将其替换为 dolapo.com,因此它会显示 www.dolapo.com/folder1.php

是否可以使用相对路径来做到这一点。

我是一名初学者编码员。我花钱请人为我做某事,但我现在想自己动手做。

I would like to convert an absolute path into a relative path.

This is what the current absolute code looks like

$sitefolder  = "/wmt/";
$adminfolder = "/wmt/admin/";

$site_path = $_SERVER["DOCUMENT_ROOT"]."$sitefolder";

// $site_path ="//winam/refiller/";

$admin_path = $site_path . "$adminfolder";

$site_url    = "http://".$_SERVER["HTTP_HOST"]."$sitefolder";
$admin_url   = $site_url . "$adminfolder";

$site_images = $site_url."images/"; 

so for example, the code above would give you a site url of

www.temiremi.com/wmt 

and accessing a file in that would give

www.temiremi.com/wmt/folder1.php

What I want to do is this I want to mask the temiremi.com/wmt and replace it with dolapo.com, so it would say www.dolapo.com/folder1.php

Is it possible to do that with relative path.

I'm a beginner coder. I paid someone to do something for me, but I want to get into doing it myself now.

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

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

发布评论

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

评论(3

旧情别恋 2024-10-09 07:04:36

问题是你的问题虽然看起来很具体,但缺少一些关键细节。

如果您发布的脚本始终被执行,并且您总是希望它转到 delapo.com 而不是 temiremi.com,那么您所要做的就是替换

$site_url    = "http://".$_SERVER["HTTP_HOST"]."$sitefolder";

$_SERVER["HTTP_HOST"]变量

$site_url    = "http://www.delapo.com/$sitefolder";

将返回所请求的任何站点的域。因此,如果用户访问 www.temiremi.com/myscript.php (假设您发布的脚本保存在名为 myscript.php 的文件中),则 $_SERVER["HTTP_HOST"] 仅返回 www.temiremi.com

另一方面,您可能并不总是重定向到同一个域,或者您可能希望脚本能够轻松适应到不同的域,而不必在将来挖掘代码层。如果是这种情况,那么您将需要一种方法来确定您希望链接到哪个域。

如果您有一个托管在 temiremi.com 上的网站,但您希望它看起来像是从 delapo.com 访问的,则这不是一个可以通过 PHP 解决的问题。您必须首先将 delapo.com 重定向到 temiremi.com 或简单地托管在 delapo.com 上。

如果情况相反,您希望在 delapo.com 上托管网站,但希望用户访问 temiremi.com,那么只需重写链接就不行了。没有一个足够复杂的答案。当用户单击链接时,此策略会将用户重定向到其他域。相反,您需要设置代理来转发信息。代理脚本的复杂程度各不相同,但最简单的脚本类似于:

<?php
    $site = file_get_contents("http://www.delapo.com/$sitefolder");
    echo $site;
?>

所以您看,我们确实需要更多关于为什么您需要此脚本及其预期目的的信息,以便为您提供帮助。

The problem is that your question, although it seems very specific, is missing some crucial details.

If the script you posted is always being executed, and you always want it to go to delapo.com instead of temiremi.com, then all you would have to do is replace

$site_url    = "http://".$_SERVER["HTTP_HOST"]."$sitefolder";

with

$site_url    = "http://www.delapo.com/$sitefolder";

The $_SERVER["HTTP_HOST"] variable will return the domain for whatever site was requested. Therefore, if the user goes to www.temiremi.com/myscript.php (assuming that the script you posted is saved in a file called myscript.php) then $_SERVER["HTTP_HOST"] just returns www.temiremi.com.

On the other hand, you may not always be redirecting to the same domain or you may want the script to be able to adapt easily to go to different domains without having to dig through layers of code in the future. If this is the case, then you will need a way to figuring out what domain you wish to link to.

If you have a website hosted on temiremi.com but you want it to look like you are accessing from delapo.com, this is not an issue that can be resolved by PHP. You would have to have delapo.com redirect to temiremi.com or simply host on delapo.com in the first place.

If the situation is the other way around and you want a website hosted on delapo.com but you want users to access temiremi.com, then simply re-writing links isn't a sophisticated enough answer. This strategy would redirect the user to the other domain when they clicked the link. Instead you would need to have a proxy set up to forward the information. Proxy scripts vary in complexity, but the simplest one would be something like:

<?php
    $site = file_get_contents("http://www.delapo.com/$sitefolder");
    echo $site;
?>

So you see, we really need a little more information on why you need this script and its intended purpose in order to assist you.

月亮邮递员 2024-10-09 07:04:36

这在 HTTP 服务器配置中会容易得多。例如,使用 Apache 的 VHost

This would be a lot easier to do in the HTTP server configuration. For example, using Apache's VHost

屋顶上的小猫咪 2024-10-09 07:04:36

我不太确定你要做什么,因为这看起来不像相对路径的绝对路径,而是一个到另一个的绝对路径。

您是否总是试图简单地将“www.temiremi.com/wmt/”更改为“delapo.com”?如果是这样,您只需要简单的字符串替换,而不是 $_SERVER 变量或路径函数。

$alteredPath = str_replace("www.temiremi.com/wmt/", "delapo.com", $oldPath);
OR
$alteredParth "www.delapo.com/" . basename($oldPath)

如果我误解了,请解释一下,我不知道你是否需要这个更加健壮/通用,你有点让我陷入了“dolapo.com”的循环(当我第一次想到你的标题时,我最初想到比较$_SERVER 中的值的路径并删除公共部分,)

如上所述,如果您只是尝试使 URL 显示为用户(在地址栏或链接中)看起来不同,PHP 无法做到这一点。

I'm not really sure what you're going for bc this doesnt look like absolute path to relative path, but rather one absolute path to another.

Are you always trying to simply change "www.temiremi.com/wmt/" to "delapo.com"? If thats the case, you just want simple string replacement rather than $_SERVER variables or path functions.

$alteredPath = str_replace("www.temiremi.com/wmt/", "delapo.com", $oldPath);
OR
$alteredParth "www.delapo.com/" . basename($oldPath)

If i misunderstand please explain, I don't know if you need this to be more robust/generic, and you kind of threw me for a loop with "dolapo.com" (when i first thought your title, i originally thought of comparing path to a value from $_SERVER and removing common parts,)

And as mentioned, if you are just trying to make the URL displayed the the user (in the address bar or links) look different PHP can't do this.

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