[英]Creating an XML sitemap with PHP
I'm trying to create a sitemap that will automatically update.我正在尝试创建一个会自动更新的站点地图。 I've done something similiar with my RSS feed, but this sitemap refuses to work.我已经做了一些与我的 RSS 提要类似的事情,但是这个站点地图拒绝工作。 You can view it live at http://designdeluge.com/sitemap.xml I think the main problem is that its not recognizing the PHP code.您可以在http://designdeluge.com/sitemap.xml实时查看我认为主要问题是它无法识别 PHP 代码。 Here's the full source:这是完整的来源:
<?php
include 'includes/connection.php';
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
<url>
<loc>http://designdeluge.com/</loc>
<lastmod>2010-04-20</lastmod>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
<url>
<loc>http://designdeluge.com/about.php</loc>
<lastmod>2010-04-20</lastmod>
<changefreq>never</changefreq>
<priority>0.5</priority>
</url>
<?php
$entries = mysql_query("SELECT * FROM Entries");
while($row = mysql_fetch_assoc($entries)) {
$title = stripslashes($row['title']);
$date = date("Y-m-d", strtotime($row['timestamp']));
echo "
<url>
<loc>http://designdeluge.com/".$title."</loc>
<lastmod>".$date."</lastmod>
<changefreq>never</changefreq>
<priority>0.8</priority>
</url>";
} ?>
</urlset>
The problem is that the dynamic URL's (eg the ones pulled from the DB) aren't being generated and the sitemap won't validate.问题是动态网址(例如从数据库中提取的网址)没有生成,站点地图也不会验证。 Thanks!谢谢!
EDIT: Right now, I'm just trying to get the code itself working.编辑:现在,我只是想让代码本身正常工作。 I have it set up as a PHP file on my local testing server.我将它设置为本地测试服务器上的 PHP 文件。 The code above is being used.上面的代码正在被使用。 Right now, nothing displays nothing on screen or in the source.现在,屏幕或源中没有任何显示。 I'm thinking I made a syntax error, but I can't find anything.我想我犯了一个语法错误,但我找不到任何东西。 Any and all help is appreciated!任何和所有的帮助表示赞赏!
EDIT 2: Ok, I got it sorted out guys.编辑 2:好的,我把它整理好了。 Apparently, I had to echo the xml declaration with PHP.显然,我不得不用 PHP 回应 xml 声明。 The final code is posted above.最后的代码贴在上面。 Thanks for your help!谢谢你的帮助!
If you take a look at the sitemap.xml
that's generated (using view source, in your browser, for example) , you'll see this :如果您查看生成的sitemap.xml
(例如,在浏览器中使用查看源代码) ,您将看到:
<?php header('Content-type: text/xml'); ?>
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http:/
...
The <?php
, present in that output, shows that PHP code is not interpreted .出现在该输出中的<?php
显示未解释 PHP 代码。
This is probably because your webserver doesn't recognize .xml
as an extension of files that should contain PHP code .这可能是因为您的网络服务器没有将.xml
识别为应该包含 PHP 代码的文件的扩展名。
At least two possible solutions :至少有两种可能的解决方案:
sitemap.php
for example, so it's interpreted by your server.将站点地图的扩展名更改为sitemap.php
,例如,它由您的服务器解释。
I would add another solution :我会添加另一个解决方案:
sitemap.php
file, that contains the code有一个包含代码的sitemap.php
文件sitemap.xml
URL actually points to the sitemap.php
file并使用 RewriteRule使sitemap.xml
URL 实际上指向sitemap.php
文件With that, you'll have the sitemap.xml
URL, which is nice (required ? ) , but as the code will be in sitemap.php
, it'll get interpreted.有了它,您将拥有sitemap.xml
URL,这很好(必需?) ,但由于代码将在sitemap.php
,因此它会被解释。
See Apache's mod_rewrite
.请参阅Apache 的mod_rewrite
。
The simplest solution is to add to your apache .htaccess
file the following line after RewriteEngine On
最简单的解决方案是在RewriteEngine On
之后添加以下行到您的 apache .htaccess
文件
RewriteRule ^sitemap\.xml$ sitemap.php [L]
and then simply having a file sitemap.php
in your root folder that would be normally accessible via http://yoursite.com/sitemap.xml
, the default URL where all search engines will firstly search.然后只需在您的根文件夹中放置一个文件sitemap.php
,该文件通常可以通过http://yoursite.com/sitemap.xml
访问,这是所有搜索引擎首先搜索的默认 URL。
The file sitemap.php
shall start with文件sitemap.php
应以
<?php header('Content-type: application/xml; charset=utf-8') ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>
I've used William's code (thanks) and with a few small modifications it worked for me.我已经使用了威廉的代码(谢谢),并进行了一些小的修改,它对我有用。
I think the line:我认为这一行:
header("Content-type: text/xml");
should be the second line after the top <?php
应该是顶部<?php
之后的第二行
Incidentally, just a small point to anyone else that copies it, but there is a single space character before the <?php
in the first line - if you inadvertantly copy it as I did, you will spend a bit of time trying to figure out why the code won't work for you!顺便说一句,对复制它的其他人来说只是一个小点,但是在第一行的<?php
之前有一个空格字符 - 如果你像我一样不小心复制它,你会花一些时间试图弄清楚为什么代码对你不起作用!
I had to tweak the MySql select statement a little bit too.我也不得不稍微调整一下 MySql select 语句。
Finally, in the output, I have used a variable $domain so that this piece of code can be used as a template without the need to think about it (provided you use the same table name each time).最后,在输出中,我使用了一个变量 $domain 以便这段代码可以用作模板而无需考虑(前提是您每次都使用相同的表名)。 The variabe is added to the connectdb.php file which is included to connect to the database.变量被添加到 connectdb.php 文件中,该文件包含在连接数据库中。
Here is my working version of the William's code:这是我的威廉代码的工作版本:
<?php
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
include 'includes/connectdb.php';
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
<url>
<loc>http://www.DOMAIN.co.uk/</loc>
<priority>1.00</priority>
</url>
<?php
$sql = "SELECT * FROM pages WHERE onshow = 1 ORDER BY id ASC";
$result = mysql_query($sql,$conn);
while($row = mysql_fetch_array($result))
{
$filename = stripslashes($row['filename']);
?>
<url>
<loc>http://www.<?php echo "$domain"; ?>/<?php echo "$filename" ?></loc>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
<?php } ?>
</urlset>
Here is the easiest way to creating and updating sitemap.xml
file.这是创建和更新sitemap.xml
文件的最简单方法。
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
require_once('database.php');
$sitemapText = '<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
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/sitemap.xsd">
<url>
<loc>http://ajkhub.com/</loc>
<lastmod>2021-08-18T18:32:09+00:00</lastmod>
<priority>1.00</priority>
</url>
<url>
<loc>http://ajkhub.com/includes/about.php</loc>
<lastmod>2021-08-18T18:32:09+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>http://ajkhub.com/includes/privacy-policy.php</loc>
<lastmod>2021-08-18T18:32:09+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>http://ajkhub.com/includes/termsandcondition.php</loc>
<lastmod>2021-08-18T18:32:09+00:00</lastmod>
<priority>0.80</priority>
</url>';
$sql = "SELECT * FROM page ORDER BY id DESC LIMIT 4";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$sitemapText .= ' <url>
<loc>'.$actual_link."/".$row['page'].'</loc>
<lastmod>'.date(DATE_ATOM,time()).'</lastmod>
<priority>0.80</priority>
</url>';
}
}
$sitemapText .= '</urlset>';
$sitemap = fopen("sitemap.xml", "w") or die("Unable to open file!");
fwrite($sitemap, $sitemapText);
fclose($sitemap);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.