复杂站点地图+索引xml文件的Apache mod_rewrite规则

时间:2013-05-09 12:02:47

标签: regex apache .htaccess mod-rewrite sitemap

我们管理的站点地图(sitemap.org)文件在500k链接范围内,这些文件经常变化,我们想要动态生成它们,不用担心,我们会将结果缓存一段时间,但它是mod_rewrite规则我遇到了问题。

由于我们有超过50k的链接,我们需要使用站点地图索引文件。站点地图和索引文件都将重定向到sitemap.php文件,该文件将使用文件名模式($_SERVER['REQUEST_URI'])来确定要显示的列表。

文件名模式如下:

www.domain.com/sitemap.index.xml
www.domain.com/sitemap.some_theme.xml
www.domain.com/sitemap.different_theme.xml

mod_rewrite还覆盖了我们的Web应用程序,因此我将包含所有内容,以防万一其他内容可能会覆盖或与我正在尝试完成的内容发生冲突:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^sitemap\.(.*)\.xml$ sitemap.php/ [NC,L]
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php/ [NC,L]
errordocument 404 /

我专门为站点地图插入的行是:

RewriteRule ^sitemap\.(.*)\.xml$ sitemap.php/ [NC,L]

否则其他所有内容都是您的标准Web应用程序。

---编辑---

好的,经过多次头疼,我发现了问题。首先,我稍微简化了规则,因为我不需要捕获模式匹配,只有正面响应,新规则是:

RewriteRule ^sitemap.*\.xml$ sitemap.php [NC,L]

所以踢球者只是交换它的顺序和下面的那个:

RewriteRule ^.*$ - [NC,L]
RewriteRule ^sitemap.*\.xml$ sitemap.php [NC,L]

我现在要打开这个问题,因为我想知道为什么这会产生影响。感谢。

1 个答案:

答案 0 :(得分:1)

此规则是多余的,应删除:

RewriteRule ^.*$ - [NC,L]

将您的代码修改为:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

ErrorDocument 404 /

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^/?sitemap\. /sitemap.php [NC,L]
RewriteRule ^ /index.php [L]