仅在部分页面上更改基本URL [英] Changing base URL on part of a page only

查看:123
本文介绍了仅在部分页面上更改基本URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个页面,可以从同一台服务器上的另一个(旧版)网站的数据库中获取并显示新闻项目。其中一些项目包含相对链接,应将其固定为指向外部网站,而不是在主网站上导致404错误。



我首先考虑在获取的新闻项目上使用< base> 标签,但这会更改整个页面的基本URL,



我正在考虑创建一个正则表达式来查找相对URL(它们都以 /index.php?)并将它们预先添加到所需的基本URL。有没有更优雅的解决方案呢?该网站建立在Symfony 2上,并使用jQuery。

解决方案

以下是我将如何解决问题: b
$ b

 函数prepend_url($ prefix,$ path){
// Prepend $ prefix to $ path if $ path不是一个完整的URL
$ parts = parse_url($ path);
返回空($ parts ['scheme'])? rtrim($ prefix,'/').'/'.ltrim($path,'/'):$ path;
}

//其他站点的URL方案和域名
$ otherDomain ='http://othersite.tld';

//创建一个DOM对象
$ dom = new DOMDocument('1.0');
$ dom-> loadHTML($ inHtml); // $ inHtml是一个从数据库获得的HTML字符串

//创建一个XPath对象
$ xpath = new DOMXPath($ dom);

//查找候选节点
$ nodesToInspect = $ xpath-> query('// * [@ src or @href]');

//循环候选节点和更新属性
foreach($ nodesToInspect as $ node){
if($ node-> hasAttribute('src')){
$ node-> setAttribute('src',prepend_url($ otherDomain,$ node-> getAttribute('src')));
}
if($ node-> hasAttribute('href')){
$ node-> setAttribute('href',prepend_url($ otherDomain,$ node-> getAttribute ( 'href' 属性)));



//找出所有要导出的节点
$ nodesToExport = $ xpath-> query('/ html / body / *');

//迭代并将它们串化
$ outHtml ='';
foreach($ nodesToExport as $ node){
$ outHtml。= $ node-> C14N();
}

// $ outHtml现在包含fixedHTML作为字符串

查看效果


I have a page on my site that fetches and displays news items from the database of another (legacy) site on the same server. Some of the items contain relative links that should be fixed so that they direct to the external site instead of causing 404 errors on the main site.

I first considered using the <base> tag on the fetched news items, but this changes the base URL of the whole page, breaking the relative links in the main navigation - and it feels pretty hackish too.

I'm currently thinking of creating a regex to find the relative URLs (they all start with /index.php?) and prepending them with the desired base URL. Are there any more elegant solutions to this? The site is built on Symfony 2 and uses jQuery.

解决方案

Here is how I would tackle the problem:

function prepend_url ($prefix, $path) {
    // Prepend $prefix to $path if $path is not a full URL
    $parts = parse_url($path);
    return empty($parts['scheme']) ? rtrim($prefix, '/').'/'.ltrim($path, '/') : $path;
}

// The URL scheme and domain name of the other site
$otherDomain = 'http://othersite.tld';

// Create a DOM object
$dom = new DOMDocument('1.0');
$dom->loadHTML($inHtml); // $inHtml is an HTML string obtained from the database

// Create an XPath object
$xpath = new DOMXPath($dom);

// Find candidate nodes
$nodesToInspect = $xpath->query('//*[@src or @href]');

// Loop candidate nodes and update attributes
foreach ($nodesToInspect as $node) {
    if ($node->hasAttribute('src')) {
        $node->setAttribute('src', prepend_url($otherDomain, $node->getAttribute('src')));
    }
    if ($node->hasAttribute('href')) {
        $node->setAttribute('href', prepend_url($otherDomain, $node->getAttribute('href')));
    }
}

// Find all nodes to export
$nodesToExport = $xpath->query('/html/body/*');

// Iterate and stringify them
$outHtml = '';
foreach ($nodesToExport as $node) {
    $outHtml .= $node->C14N();
}

// $outHtml now contains the "fixed" HTML as a string

See it working

这篇关于仅在部分页面上更改基本URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆