简体   繁体   中英

.htaccess rewrite sitemap php to xml

I want to create a dynamic sitemap.xml file using php and .htaccess from my database. My sitemap.php file:

<?
include "db.php";
header("Content-Type: text/xml;charset=iso-8859-1");  
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
";

{
while($row = mysql_fetch_array($result)) { 
  $url = $row["url_code"];
echo "   <url>
      <loc>http://domain.com/$url</loc>
  </url>
";
}
}
?>
</urlset>

I have the following .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^stats [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
Rewriterule ^sitemap.xml$ sitemap.php [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . index.php [L]

But all I get is a 404 not found error. I have tried just:

RewriteEngine On
Rewriterule ^sitemap.xml$ sitemap.php [L]

But get the same error. Where am I going wrong?

try this:

RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
RewriteRule ^sitemap$ sitemap.php [L]

Everything seems to work now, without changing anything! Accordingly this is how you do it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM