【问题标题】:Using .htaccess to redirect /sitemap.xml to /index.php/sitemap/使用 .htaccess 将 /sitemap.xml 重定向到 /index.php/sitemap/
【发布时间】:2013-05-08 02:30:32
【问题描述】:

我正在使用 codeigniter 生成一个包含 XML 数据的页面。该文件的 URL 位于 /index.php/sitemap,但我希望通过 /sitemap.xml 访问该 URL

我的.htaccess 文件已经被修改为允许用户访问 URL,而无需在 URL 之前输入 /index.php/。这是我正在使用的.htacess 文件:

RewriteEngine On

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$ index.php/sitemap [L]

一切正常,除了 sitemap.xml 文件的重定向。我在运行 XAMPP 的 Windows 机器上

我做错了什么?

【问题讨论】:

  • 您可能希望将 xml 行移到索引规则上方,因为该行将首先匹配。
  • 我发现 codeigniters URL 路由方法可以工作,但为了将来参考,将此规则放在顶部给了我 404 错误。

标签: .htaccess codeigniter mod-rewrite


【解决方案1】:

跳过 .htaccess 修改并改用普通路由。

$route['sitemap\.xml'] = 'sitemap';

【讨论】:

  • 谢谢,这成功了。我不知道为什么我没有想到使用 codeigniters URL 路由。
【解决方案2】:

规则的顺序很重要。规则按照编写顺序进行处理。模式.* 匹配所有内容,因此匹配规则

RewriteRule ^.*$ index.php [NC,L]

在所有请求到达最后一条规则之前重写所有请求。

当您将sitemap.xml 规则放在顶部时,它将按预期工作。

【讨论】:

  • 我发现 codeigniters URL 路由方法可以工作,但为了将来参考,将此规则放在顶部给了我 404 错误。
【解决方案3】:
RewriteRule ^.*sitemap\.xml$ index.php/sitemap [L]
RewriteRule ^(.*)$ /index.php/$1 [L]

您在到达站点地图检查之前进行匹配,因此它始终重定向到 index.php

这是来自user guide的示例:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^.*/(.*)$ /index.php/$1 [L]

您可以在标准 index.php 重写之前将站点地图检查推到那里

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^.*/sitemap\.xml$ index.php/sitemap [L]
RewriteRule ^.*/(.*)$ /index.php/$1 [L]

【讨论】:

  • 由于某种原因,使用用户指南中的示例在我的本地主机上出现 500 服务器错误,所以我不得不使用我在 codeigniter 论坛上找到的另一个 .htaccess 文件
  • 好的,所以只需将 XML 重写放在 index.php 重写之前
  • 我发现 codeigniters URL 路由方法可以工作,但为了将来参考,将此规则放在顶部给了我 404 错误。
  • 是的;缺少一个正斜杠。 RewriteRule ^.*/(.*)$ /index.php/$1 [L]
猜你喜欢
  • 1970-01-01
  • 2013-01-21
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多