gpt4 book ai didi

php - 静态站点批量内链重定向方法

转载 作者:行者123 更新时间:2023-11-29 12:20:01 24 4
gpt4 key购买 nike

我有一个包含大约 1500 多个页面的静态网站。这些 HTML 页面中的每一页都有一个内部链接(我的意思是在页面本身上编码),我想将其替换为新链接。

但是,不可能编辑所有这些页面,因为有数千个页面。

这是一个例子:

  • 假设我有一个页面 www.example.com/page.html
  • 该页面上有一个超链接,可指向 www.abc.com
  • 我希望将此链接替换/重定向为 www.xyz.com 而不是 www.abc.com

我已经有一个 MySQL 表,其中两列中包含所有当前链接和新链接。

有没有办法通过使用 .htaccess 文件、PHP 和 MySQL 来重定向所有这些链接?如果可能的话,代码会是什么样子?

最佳答案

我找到了一种使用此脚本更改所有链接的方法。

<?php
require_once("connection.inc.php");

function get_value($file){
//$html = file_get_contents($file,true);
$dom = new DOMDocument('1.0');
$dom->loadHTMLFile($file);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//a/@href');
foreach($nodes as $href) {
return $href->nodeValue; // echo current attribute value
//$href->nodeValue = 'new value'; // set new attribute value
//$href->parentNode->removeAttribute('href'); // remove attribute
}
}

function put_value($file,$pattern){
//$html = file_get_contents($file,true);
$dom = new DOMDocument('1.0');
$dom->loadHTMLFile($file);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//a');
$nodes->item(0)->setAttribute("href",$pattern);
$dom->save($file);
}

$dir = new RecursiveDirectoryIterator(__DIR__);
$flat = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($flat, '/\.html$/i');

foreach($files as $file) {
$value=get_value($file);
$query=mysql_query("SELECT new_links FROM site WHERE current_links='$value'");
if(mysql_num_rows($query)==1){
$value_orginal = mysql_result($query,0);
put_value($file,$value_orginal);
echo "The ".$file." has changed to ".$value."</br>";
}
else{
die("die baby die :)");
}
}
?>

关于php - 静态站点批量内链重定向方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29161404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com