使用htaccess来“伪造” XML 文件?
这是我试图解决的问题:我有一个动态 php 驱动的网站,该网站不断更新新内容,并且我希望我的 XML 站点地图自动保持最新。我看到两个选项:
- 编写一个 php 脚本来查询我的数据库以获取我的所有内容并将其输出到 http://mysite。 com/sitemap.xml,使用 cron 作业定期执行脚本。
- 只需将我的站点地图创建为 php 文件 (sitemap.php),查询数据库并直接写入该文件,然后使用 htaccess 重写规则
RewriteRule ^sitemap.xml$ sitemap.php
以便每当有人请求 sitemap.xml,它们将被定向到 php 文件并获取新的站点地图文件。
我宁愿选择选项#2,因为它更简单并且不需要设置 cron,但我想知道如果 sitemap.xml 实际上是一个 php 文件,Googlebot 是否不会将其识别为有效?
有谁知道选项 #2 是否可行,如果不行,是否有更好的方法来自动创建最新的 sitemap.xml 文件?我真的很惊讶我在这方面遇到了这么多麻烦......谢谢!
Here's the problem I'm trying to solve: I have a dynamic php-driven website that is constantly being updated with new content, and I want my XML sitemap to stay up to date automatically. Two options I see:
- Write a php script that queries my database to get all my content and outputs to http://mysite.com/sitemap.xml, execute the script regularly using a cron job.
- Simply create my sitemap as a php file (sitemap.php), query the db and write directly to that file, and use the htaccess rewrite rule
RewriteRule ^sitemap.xml$ sitemap.php
so that whenever someone requests sitemap.xml they're directed to the php file and get a fresh sitemap file.
I'd much rather go with option #2 since it's simpler and doesn't require setting up a cron, but I'm wondering if Googlebot will not recognize sitemap.xml as valid if it's actually a php file?
Does anyone know if option #2 would work, and if not whether there's some better way to automatically create an up-to-date sitemap.xml file? I'm really surprised how much trouble I've had with this... Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需确保您的脚本生成适当的
Content-Type
标头即可。您可以使用 header() 来完成此操作。Just make sure your script generates the appropriate
Content-Type
header. You can do so with header().Google 只会获取响应的标头和正文。如果您的 php 脚本返回的标头和正文与您的 Web 服务器返回的标头和正文相同,那么从技术上讲,PHP 脚本响应或服务器的 XML 文件响应之间没有区别。使用
curl -i http://example.com/
检查 a 的响应标头如果您想自行测试,请询问。所以你可以安全地这样做,这就是 mod_rewrite 的设计目的(除了许多其他东西)。
Google will only get the headers and the body of the response. If your php script returns the same headers and the same body as your webserver would return, then there is technically no difference between the PHP script response or the XML file response by your server. Use
curl -i http://example.com/
to inspect the response headers of a request if you would like to test that on your own.So you can safely do this, that's for what mod_rewrite has been designed (next to the many other things).