简体   繁体   English

如何在localhost和Web服务器上实现绝对URL?

[英]How to implement absolute URLs on localhost and web server?

I've typically used the following linking practice, of relative URLs with an absolute URL path : 我通常使用以下链接实践, 相对URL与绝对URL路径

<a href="/relative/path/to/document.html">

But I will implement absolute URLs : 但我将实现绝对URL

<a href="http://example.com/relative/path/to/document.html">

It's not a problem for me to change them (automated find & replace in HTML documents). 对我来说,改变它们并不是问题(HTML文档中的自动查找和替换)。

But what is the best practice to make sure that it will work on both my localhost (which supports PHP), as well as on the web ? 但是确保它可以在我的localhost (支持PHP)以及Web上运行的 最佳实践是什么 And why? 为什么?


For example, here's how I do PHP includes: 例如,这里是我如何做PHP包括:

<?php include($_SERVER['DOCUMENT_ROOT']."/relative/path/to/document.html"); ?>

Adopt the same approach for href URLs? href网址采用相同的方法? Is a different PHP technique better? 不同的PHP技术更好吗? Curious to hear why. 很想知道为什么。

Keep the same directory structure locally and on the web host, and use relative URIs and paths whenever possible. 在本地和Web主机上保留相同的目录结构,并尽可能使用相对URI和路径。 If you must use an absolute URI, define it once somewhere and use the constant whenever you need to output a URI to avoid having to make the changes using find and replace. 如果必须使用绝对URI,请在某处定义它,并在需要输出URI时使用常量,以避免必须使用查找和替换进行更改。

<?php
    define('ROOT_URI', 'http://example.com/subdirectory');
?>

<a href='<?php echo ROOT_URI; ?>/relative/path/to/document.html'> document </a>

It's easier if you have a configuration file or some other included file where you only define it once. 如果您有一个配置文件或其他包含文件,您只需定义一次就会更容易。

You could just include a predefined constant or variable in the HTML hrefs. 您可以在HTML hrefs中包含预定义的常量或变量。

eg 例如

<?php
define("LOCAL", "http://localhost");
define("WEB", "http://foo.bar");
$environment = LOCAL; //change to WEB if you're live
?>
<a href="<?php echo $environment; ?>/relative/path/to/document.html">

That all depends on how you plan on hosting the content. 这一切都取决于您计划如何托管内容。 PHP is not a bad choice as there are oodles of hosting companies that support PHP. PHP并不是一个糟糕的选择,因为有大量的托管公司支持PHP。 Without a scripting language like PHP, your best bet would be javascript, but depending why you are trying to make the URL's absolute, that might defeat your purpose. 如果没有像PHP这样的脚本语言,你最好的选择就是javascript,但这取决于你为什么试图使URL绝对,这可能会破坏你的目的。

One thing I would suggest is making a configuration file as you may or may not be able to depend on $_SERVER['DOCUMENT_ROOT']. 我建议的一件事是制作配置文件,因为您可能或可能无法依赖$ _SERVER ['DOCUMENT_ROOT']。

Say: config.php 说:config.php

<?php
define("HOST", "http://example.com");
// Whatever else you might need on all files.

Then every file that needs it, put this at the top 然后每个需要它的文件,把它放在顶部

<?php
include_once(dirname(__FILE__)."/config.php");

// Keep in mind this path is relative to current file, so if you are 3 sub-folder keep
// include_once(dirname(__FILE__)."/../../../config.php")
?>

...

<a href=<?php echo HOST;?>/relative/path/to/document.html">...

Instead of doing this, you can add below line to your head tag. 您可以在头标记下方添加以下行,而不是这样做。 Now you can give all urls relative to your root. 现在,您可以根据您的根提供所有网址。 Now, when you upload to server or work on localhost. 现在,当您上传到服务器或在localhost上工作时。 Just change url here. 只需在这里更改网址。

<base href="http://mydomin.com/" />

Here is a solution that works both on remote and local servers and doesn't require to change the domain name: 这是一个既适用于远程服务器又适用于本地服务器的解决方案,不需要更改域名:

$protocol = isset($_SERVER["HTTPS"]) ? 'https://' : 'http://';
$url_base = $protocol . $_SERVER['HTTP_HOST'];

With these 2 lines you won't need to change the code if you change your domain name and also you won't need to change it if you switch between HTTP and HTTPS. 使用这两行代码,如果更改域名,则无需更改代码,如果在HTTP和HTTPS之间切换,则无需更改代码。 Feel free to change variable names as you prefer. 您可以根据需要随意更改变量名称。

So then your code for links would look something like this: 那么你的链接代码看起来像这样:

<a href="<?php echo $url_base; ?>/relative/path/to/document.html">link</a>

IMPORTANT UPDATE REGARDING SECURITY: 关于安全的重要更新:

There is however a security risk with this solution. 但是,此解决方案存在安全风险。 Here's an article on it . 这是一篇关于它文章

The $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] variables can be changed by sending a different Host header when accessing the site: 可以通过在访问网站时发送不同的主机标头来更改$_SERVER['HTTP_HOST']$_SERVER['SERVER_NAME']变量:

curl -H "Host: notyourdomain.com" http://yoursite.com/

Doing that, any URLs that used $_SERVER['HTTP_HOST'] or $_SERVER['SERVER_NAME'] would use notyourdomain.com. 这样做,任何使用$_SERVER['HTTP_HOST']$_SERVER['SERVER_NAME']网址都会使用notyourdomain.com。

Taking this further, imagine an attacker fills out a password reset form with your email and changes the Host header. 进一步考虑,假设攻击者用您的电子邮件填写密码重置表单并更改主机标头。 The password reset email would then direct you to their site. 密码重置电子邮件将引导您访问他们的网站。 If you're paying attention to the domain, you're fine, but normal users don't and that's why phishing attacks work. 如果您关注域名,那么您很好,但普通用户不会这样做,这就是网络钓鱼攻击的原因。

So use it with caution or better avoid using it. 因此请谨慎使用或更好地避免使用它。

Here's a way to deal with multiple environments from the linked article: 这是一种处理链接文章中的多个环境的方法:

When dealing with multiple environments, you can use $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] , but only to check which domain you're on and then manually set the correct URL. 处理多个环境时,您可以使用$_SERVER['HTTP_HOST']$_SERVER['SERVER_NAME'] ,但只能检查您所在的域,然后手动设置正确的URL。 You could keep it simple with an array of valid domains: 您可以使用一系列有效域来保持简单:

 $domains = array('domain.com', 'dev.domain.com', 'staging.domain.com', 'localhost'); if (in_array($_SERVER['HTTP_HOST'], $domains)) { $domain = $_SERVER['HTTP_HOST']; }else { $domain = 'localhost'; } 

You can also make it a bit shorter: 你也可以缩短一点:

$domains = array('domain.com', 'dev.domain.com', 'staging.domain.com', 'localhost');
$domain = in_array($_SERVER['HTTP_HOST'], $domains) ? $_SERVER['HTTP_HOST'] : 'localhost';

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

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