2017-01-19 27 views
2

我需要僅使用HTML DOM將所有相對URL替換爲網頁的絕對URL。這是可能的?如何使用HTML DOM將所有相對URL都設置爲絕對URL?

我也在使用yii2。

網站:http://www.example.com

<link href="/css/site.css" rel="stylesheet"> 
<a href="/page">Page</a> 

結果:

<link href="http://www.example.com/css/site.css" rel="stylesheet"> 
<a href="http://www.example.com/page">Page</a> 
+0

您的問題添加了相關的相關代碼和一個樣品預期的結果 – scaisEdge

+0

更新添加一個'基地'標籤? – pguardiario

回答

2

集$方案參數設置爲true,如果你要鏈接到一個控制器動作返回一個絕對的URL。

Url::to(['my_controller/action'], true);

http://www.yiiframework.com/doc-2.0/yii-helpers-baseurl.html#to()-detail

對於文件,您可以使用此Html::a($text, $url = null, $options = [])下面的選項

// A normal string. This will use the exact string as the href attribute 
$url = 'images/logo.png'; // This returns href='images/logo.png' 

// A string starting with a Yii2 alias 
$url = '@web/images/logo.png' // This returns href='http://www.example.com/images/logo.png' 
0

您colud使用網址幫手

假設你有你的/ yii2 /網頁CSS/CSS

<?php 
    use yii\helpers\Url; 
echo '<link href="' .Url::base() . '/css/site.css' .'" rel="stylesheet">'; 

假設你有你的/ yii2/CSS CSS

echo '<link href="' .Url::base() . '../css/site.css' .'" rel="stylesheet">';