php中将绝对路径转换为相对路径
我想将绝对路径转换为相对路径。
这就是当前绝对代码的样子
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是你的问题虽然看起来很具体,但缺少一些关键细节。
如果您发布的脚本始终被执行,并且您总是希望它转到
delapo.com
而不是temiremi.com
,那么您所要做的就是替换$_SERVER["HTTP_HOST"]
变量将返回所请求的任何站点的域。因此,如果用户访问
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
,那么只需重写链接就不行了。没有一个足够复杂的答案。当用户单击链接时,此策略会将用户重定向到其他域。相反,您需要设置代理来转发信息。代理脚本的复杂程度各不相同,但最简单的脚本类似于:所以您看,我们确实需要更多关于为什么您需要此脚本及其预期目的的信息,以便为您提供帮助。
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 oftemiremi.com
, then all you would have to do is replacewith
The
$_SERVER["HTTP_HOST"]
variable will return the domain for whatever site was requested. Therefore, if the user goes towww.temiremi.com/myscript.php
(assuming that the script you posted is saved in a file calledmyscript.php
) then$_SERVER["HTTP_HOST"]
just returnswww.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 fromdelapo.com
, this is not an issue that can be resolved by PHP. You would have to havedelapo.com
redirect totemiremi.com
or simply host ondelapo.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 accesstemiremi.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: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.
这在 HTTP 服务器配置中会容易得多。例如,使用 Apache 的 VHost
This would be a lot easier to do in the HTTP server configuration. For example, using Apache's VHost
我不太确定你要做什么,因为这看起来不像相对路径的绝对路径,而是一个到另一个的绝对路径。
您是否总是试图简单地将“www.temiremi.com/wmt/”更改为“delapo.com”?如果是这样,您只需要简单的字符串替换,而不是 $_SERVER 变量或路径函数。
如果我误解了,请解释一下,我不知道你是否需要这个更加健壮/通用,你有点让我陷入了“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.
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.