简体   繁体   English

PHP网站的绝对URL路径

[英]Absolute URL Path for PHP Website

I am working on a website in a CMS site. 我正在CMS网站上的网站上工作。 I have provided a link in my footer file eg blogs.php. 我在我的页脚文件中提供了一个链接,例如blogs.php。 This page is at the root like " http://www.example.com/new_cms/blogs.php " 此页面的根目录为“ http://www.example.com/new_cms/blogs.php

When I moves to other files link like " http://www.example.com/new_cms/forums " the footer link is changed to " http://www.example.com/new_cms/forums/blogs.php " but the blogs.php page resides at path " http://www.example.com/new_cms/ " . 当我移至“ http://www.example.com/new_cms/forums ”之类的其他文件链接时,页脚链接更改为“ http://www.example.com/new_cms/forums/blogs.php ”,但是blogs.php页面位于路径“ http://www.example.com/new_cms/ ”。 I tried different $_SERVER[] variables but got no luck to get the above path " http://www.example.com/new_cms/ " means the server name with the directory where the project is currently running from. 我尝试了不同的$ _SERVER []变量,但没有运气获得上述路径“ http://www.example.com/new_cms/ ”表示服务器名称以及当前从中运行项目的目录。

Any one have idea how to get it done will be a great help. 任何人都知道如何完成它将是一个很大的帮助。

Thanks 谢谢

This is probably because your link has a relative url in it, like so : 这可能是因为您的链接中包含相对网址,如下所示:

<a href="blogs.php">Blogs</a>

So it looks for the blogs.php file in the current directory. 因此它将在当前目录中查找blogs.php文件。

If you want that link to point to the blogs.php file that is in the new_cms folder, you have to use a link like this : 如果您希望该链接指向new_cms文件夹中的blogs.php文件,则必须使用如下链接:

<a href="/new_cms/blogs.php">Blogs</a>

You can obtain the server name from $_SERVER['SERVER_NAME'] that is the canonical server name specified in the server/virtual host configuration. 您可以从$_SERVER['SERVER_NAME']获取服务器名称,该名称是服务器/虚拟主机配置中指定的规范服务器名称。 Besides that $_SERVER['HTTP_HOST'] gives you the host specified in the HTTP request header field Host . 除此之外, $_SERVER['HTTP_HOST']为您提供在HTTP请求标头字段Host中指定的主机 In general, these two values are identical. 通常,这两个值是相同的。 But since both can be manipulated by user input (see Chris Shiflett's SERVER_NAME Versus HTTP_HOST ) you need to be careful when using these. 但是由于可以通过用户输入(请参见Chris Shiflett的SERVER_NAME与HTTP_HOST )来操纵两者,因此在使用它们时需要小心。

As for the path, you will probably need to specify the base URL path on our own. 至于路径,您可能需要自行指定基本URL路径。 Because if you think of URL rewriting techniques, the physical path (file system path) does not need to be the same as the logical path (URL path). 因为如果考虑URL重写技术,则物理路径(文件系统路径)不需要与逻辑路径(URL路径)相同。 So you can not derive the logical base path from the requested URL path, the script file path and the document root. 因此,您不能从请求的URL路径,脚本文件路径和文档根目录派生逻辑基础路径。

您可以使用绝对URL或尝试使用条件重写URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM