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

2019-05-21 05:28发布

问题:

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>

回答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



回答2:

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



回答3:

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

header('Content-Type: text/xml');