PHP - 创建包含动态内容的 xml 站点地图
这可能吗?我有 4 个页面,当单击相应的菜单项时,它们会使用 PHP/AJAX 动态加载。
优化 PHP 动态加载内容的最佳实践是什么?我尝试生成一个基本的 xml 站点地图,但显示的唯一页面是具有页面相对路径的后端处理程序,而不是页面本身。
感谢各位的回复,谢谢。
Is this possible? I have 4 pages that load dynamically with PHP/AJAX when corresponding menu items are clicked.
What is the best practice for optimizing content loaded dynamically with PHP? I have tried generating a basic xml sitemap but the only page that shows is the back end handler with relative paths to the pages, not the pages themselves.
Appreciate the responses, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,Ajax 链接的最佳实践是,您仍然在非 js 浏览器可以跟踪的标签中指定一个地址(搜索引擎)。这样做的一个令人愉快的副作用(除了 SEO 之外)是,创建 XML 站点地图就像没有动态加载 Ajax 内容一样简单......
您应该努力使 Ajax 内容在没有 JavaScript 的情况下正常运行。那么大多数此类问题就会消失。
基于评论进行更新
根据以下评论中的进一步说明,您有几个选项...
最简单的方法是在动态内容的缓存版本中实现一致的命名约定。然后你可以简单地使用
glob
文档(或opendir
docs 和readdir
docs)并根据在目录中找到的文件的名称。另一种选择是创建某种“注册表”,每当缓存动态页面时都会更新该“注册表”。这可以是数据库表或将站点地图文件名标识符映射到实际缓存文件名的平面文本文件。然后,每当存储或删除缓存文件时,您都可以在此注册表中添加或删除条目。
作为我刚才提到的简单注册表的替代方案,您可以使用 XML 站点地图本身作为注册表。每次创建缓存文件时,您都可以将
sitemap.xml
加载到DOMDocument
docs 并实时操作站点地图,并在完成编辑后将其保存回磁盘。我认为这实际上只是解决问题。
Well, the best practice with Ajax links is that you still specify an address in the tags that non-js browsers can follow (search engines). A pleasant side effect of this (besides SEO) is that an XML site map would be just as simple to create as if there were no Ajax content loaded dynamically ...
You should strive to make your Ajax content function normally in the absence of JavaScript. Then most of these kinds of issues will disappear.
UPDATE BASED ON COMMENTS
You have a couple of options based on the further explication in the below comments ...
The simplest would be to implement consistent naming conventions among the cached versions of your dynamic content. Then you could simply scan the cache directory using
glob
docs (oropendir
docs andreaddir
docs) and generate the sitemap based on the names of files found in the directory.Another option would be to create a "registry" of some kind that is updated whenever you cache a dynamic page. This could be a database table or a flat text file that maps the sitemap filename identifier to the actual cached file name. You would then add or remove entries from this registry whenever a cached file was stored or deleted.
As an alternative to a simple registry like I just mentioned you might use the XML sitemap itself as the registry. Each time a cached file was created, you could load
sitemap.xml
into aDOMDocument
docs and manipulate the sitemap in real time, saving it back to disk when you finish editing.It really just comes down to problem solving, I think.