ryerye
ryerye

Reputation: 123

.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?

Upvotes: 0

Views: 7647

Answers (2)

mostafa
mostafa

Reputation: 31

try this:

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

Upvotes: 3

ryerye
ryerye

Reputation: 123

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

Upvotes: 0

Related Questions