Search code examples
phpyii2simple-html-dom

How can you put all relative URL to absolute URL using HTML DOM?


I need to replace all relative URL to absolute URL of a web page using only HTML DOM. It's possible?

I am also using yii2.

Example

Site:http://www.example.com

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

Result:

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

Solution

  • set $scheme parameter to true to return an absolute url if you want to link to an controller action.

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

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

    For files you can use this Html::a( $text, $url = null, $options = [] ) with below 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'