.htaccess URL re-write (sitemap.xml to sitemap.php)

10,383

Solution 1

You could do it explicitly:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteBase /

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

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>

You'll likely run into problems doing this though... You should create exclusions for spiders so they still receive the sitemap.xml

Solution 2

RewriteRule ^sitemap.xml$ public/sitemap.php [L]

Solution 3

make sure your php file loads as xml, using the code bellow

header('Content-Type: text/xml');
Share:
10,383
Darren Parker
Author by

Darren Parker

Updated on June 04, 2022

Comments

  • Darren Parker
    Darren Parker almost 2 years

    below is my current .htaccess file. I would like to add another condition/rule where if a request for sitemap.xml is made, sitemap.php is served instead. Please help : )

    Options +FollowSymLinks
    
    <IfModule mod_rewrite.c>
    
       RewriteEngine On
    
       RewriteBase /
    
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteCond %{REQUEST_FILENAME} !-d
    
       RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
    
    </IfModule>
    
  • Kai Qing
    Kai Qing almost 11 years
    Wouldn't that apply those conditions to the xml line and not to the initial rewrite? My htaccess recolection is kind of hazy but I would have assumed the new rule would have to come before the conditions for index were stated
  • stslavik
    stslavik almost 11 years
    Point taken. Good catch; I wasn't considering that it'd stop after the %{REQUEST_FILENAME} !-f
  • Darren Parker
    Darren Parker almost 11 years
    Ok, so should I try with this new rule after "RewriteCond %{REQUEST_FILENAME} !-f" I have a .php file that produces a sitemap dynamically but want crawlers to be able to find use sitemap.xml to view its content.
  • stslavik
    stslavik almost 11 years
    Hrm... Personally, if I were using a dynamic sitemap generator (and I don't use site maps at all for various reasons), I would design it to re-spider the website and output a new .xml file when I add new content, and I would keep it disconnected from public areas. Then the point about spiders would be moot.