【问题标题】:How to redirect and remove index.php in Codeigniter [duplicate]如何在 Codeigniter 中重定向和删除 index.php [重复]
【发布时间】:2013-05-20 04:22:00
【问题描述】:

我正在使用下面的 htaccess 从我在 Codeigniter 中的 url 中删除 index.php。

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

问题是用户仍然可以使用下面的代码来访问该方法:

http://example.com/index.php/controller/method

如果他们添加了 index.php,我如何将它们 重定向 到没有 index.php 的 url 或将它们发送到 error 页面?

【问题讨论】:

  • 本题重复标注错误。这是一个后续案例,提问者既希望删除 index.php,又希望将任何现有链接/书签重定向到新 URL。相关问题并没有告诉您如何做第二部分 - 这是提问者明确询问的内容。

标签: php codeigniter .htaccess mod-rewrite redirect


【解决方案1】:
RewriteEngine On

RewriteRule ^index.php/(.*)$ /$1 [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]

第一条规则永久重定向用户非 index.php 站点,然后它涉及您的第二条规则和宾果游戏。

【讨论】:

  • 将 301 更改为 302 以保存搜索引擎中的 SEO 链接匹配。我会删除我的答案,你的有重定向。
  • 现在我收到此页面有重定向循环的错误
  • @rcpayan:将您的最后一条规则更改为RewriteRule ^(.*)$ index.php?/$1 [L]。 Apache 将 index.php/$1 视为重定向。
  • @user181892:这最后一条评论应该修复循环:-)
  • @SébastienRenauld 非常感谢。
猜你喜欢
  • 2020-05-26
  • 1970-01-01
  • 2019-11-23
  • 2016-07-02
  • 2015-03-19
  • 2013-05-09
  • 2011-02-21
  • 2021-01-22
  • 2020-08-12
相关资源
最近更新 更多