首页 > 解决方案 > 如何将所有对 index.php 和 sitemap.xml 的请求重写为 sitemap.php

问题描述

请帮忙,我已经做了2天了,一切都是徒劳的......

此代码适用于将目录和页面重写为一个 index.php 页面

    DirectoryIndex /index.php
    RewriteEngine on
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d

    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.php [NC,L]

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

sitemap.xml 的问题,因为它必须重写为 sitemap.php 而不是 index.php

标签: .htaccessmod-rewrite

解决方案


您需要将站点地图规则放在索引规则之前。规则是从上到下处理的,一旦它达到索引规则(因为.*会 match sitemap.xml),它将因为[L]标志而停止进一步处理。

另一种选择是只使用 php 和 inside index.phpinclude sitemap.php;$_SERVER['REQUEST_URI']equals时sitemap.xml。结果相同。


推荐阅读