我现在正在做类似的事情,将一个巨大的静态 html 存储迁移到在 django 上运行(这很痛苦和血腥)。
我们的解决方案并不是特别优雅。在每个页面的迁移过程中,我们记录旧 url,然后是新 url,并将它们添加到重定向数据库。将所有内容迁移到新的后端和 url 结构后,我们将运行一个脚本,该脚本将使用这些 xpath 选择器识别文档中的所有链接:
//a/@href
//img/@src
接下来,我们从重定向表中提取重定向并将链接替换为下面的正则表达式。
#escape special characters to avoid problems with the regex
link = link.replace('#', r'\#')
link = link.replace('.', r'\.')
link = link.replace('/', r'\/')
link = link.replace(':', r'\:')
#compile a regex, using the source link, and replace all existing links
repl_regex = r'href\s{0,}\=[\s\"\']{0,}(%s)[\s\"\']{0,}'%link
markup = re.sub(repl_regex, 'href="%s"'%dst_url, markup)
#repeat for images
repl_regex = r'src\s{0,}\=[\s\"\']{0,}(%s)[\s\"\']{0,}'%link
markup = re.sub(repl_regex, 'src="%s"'%dst_url, markup)
#Let me know if you have any questions, the above is written in python
#and it sounds like you're using php and a .net language.
现在,虽然这种方法可能比您想要的更多,并且需要更多的前期准备,但它有两个优点:
1) 通过将文档中的每个链接与重定向表进行比较,您将能够更轻松地识别丢失的页面/丢失的重定向
2) 搜索引擎优化。无需让 googlebot 重新抓取您的整个网站,只需针对您的重定向表提供 301 重定向
如果您有任何问题,请告诉我。