【问题标题】:php make xml code but browser open in htmlphp制作xml代码但浏览器以html打开
【发布时间】:2016-07-21 21:15:51
【问题描述】:

我创建页面http://omenorpreco.com/sitemap_index.xml 供谷歌阅读并识别我的所有页面。

但是,当访问此页面时,XML 不会以 xml 模式打开。

如果我查看此页面的源代码,复制并粘贴到我桌面上的文件中并在browser中打开,则此文件正确打开

怎么了?

  <?php
    echo "<?xml version='1.0' encoding='UTF-8'?>";?>
        <sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
        header("Content-Type: application/xml; charset=UTF-8");
        $sql = "SELECT ceil(count(*)/5000) as total FROM `produto_price`";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
?>
        <sitemap>
            <loc>http://omenorpreco.com/sitemap-categoria.xml</loc>
            <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        </sitemap>
<?php
        for($i = 1; $i <= $row['total']; $i++) : ?>
            <sitemap>
                <loc>http://omenorpreco.com/sitemap-<?php echo $i ; ?>.xml</loc>
                <lastmod><?php echo date('Y-m-d'); ?></lastmod>
            </sitemap>
<?php 
        endfor 
?>
    </sitemapindex>

我的 .htaccess 文件重定向到 url_amigavel.php

而url_amigavel.php包含文件sitemap_index.php

【问题讨论】:

  • 标题首先,然后是输出。
  • @fusion3k 将其添加为答案,以便我投票。
  • Output raw XML using php的可能重复

标签: php xml


【解决方案1】:

试试这个

<?php 
    header("Content-Type: application/xml; charset=UTF-8");
    echo "<?xml version='1.0' encoding='UTF-8'?>";
    $sql    = "SELECT ceil(count(*)/5000) as total FROM `produto_price`";
    $result = mysql_query($sql);
    $row    = mysql_fetch_array($result);
?>
<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>http://omenorpreco.com/sitemap-categoria.xml</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    </sitemap>
<?php for($i = 1; $i <= $row['total']; $i++): ?>
    <sitemap>
        <loc>http://omenorpreco.com/sitemap-<?php echo $i ; ?>.xml</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    </sitemap>
<?php endfor; ?>
</sitemapindex>

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2012-08-17
    相关资源
    最近更新 更多