【问题标题】:Remove and Redirect index.php from CodeIgniter从 CodeIgniter 中删除并重定向 index.php
【发布时间】:2020-05-26 21:17:28
【问题描述】:

我有一个旧项目,它有一些奇怪的问题。它的 CodeIgniter 项目(版本 2.X)。

Google 正在使用 index.php? 为我们的 URL 编制索引。下面是一个例子

https://www.website.com/index.php?/my-seo-friendly-uri

我可以从上面的 url 看到我的页面,还有 2 个变体如下

https://www.website.com/index.php/my-seo-friendly-uri

https://www.website.com/my-seo-friendly-uri

我们在整个网站中都使用了https://www.website.com/my-seo-friendly-uri

我的问题是如何将https://www.website.com/index.php?/my-seo-friendly-urihttps://www.website.com/index.php/my-seo-friendly-uri 重定向到https://www.website.com/my-seo-friendly-uri

我已经做过的事情

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index.php/(.*)$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L] 
</IfModule>

这会将index.php 重定向到普通网址,但index.php? 版本的网址仍然没有重定向

在我的config.php 中,我有以下设置

$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

【问题讨论】:

  • index.php? 版本上使用[R=301,L]
  • 密码是什么?
  • 您检查过配置中的$config['enable_query_string'] 设置吗?
  • 没有这个设置

标签: php .htaccess codeigniter redirect


【解决方案1】:
https://www.example.com/index.php?/my-seo-friendly-uri

此 URL 包含一个查询字符串,因此需要稍微不同的规则才能匹配它。 RewriteRule pattern 只匹配 URL 路径(在这种情况下只是 index.php)。查询字符串在其自己的变量中可用。

在现有指令之前添加以下内容(除了匹配 /index.php/my-seo-friendly-url 的指令 - 作为 path-info 传递):

# Redirect URLs of the form "/index.php?/my-seo-friendly-uri"
# And "/?/my-seo-friendly-uri"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(/.*)
RewriteRule ^(index\.php)?$ %1 [QSD,R=302,L]

查询字符串被捕获(第二个条件),反向引用(%1)用于构造重定向。

为了防止重定向循环,需要检查REDIRECT_STATUS 环境变量的第一个条件,因为您似乎正在使用查询字符串方法在以后的重写中路由codeigniter URL。 REDIRECT_STATUS env var 在初始请求中为空,但在第一次成功重写后设置为“200”(如 200 OK HTTP 状态)。

QSD 标志 (Apache 2.4+) 需要从重定向请求中丢弃原始查询字符串。如果您仍在使用 Apache 2.2,则将 ?(空查询字符串)附加到 substitution 字符串。 IE。 %1?

通过匹配index.php 可选(即^(index\.php)?$),它还将规范化省略index.php但仍包含查询字符串的URL(当前可能有也可能没有是个问题)。例如。 /?/my-seo-friendly-uri.

请注意,这目前是 302(临时)重定向(与您现有的重定向一样)。只有在确认它可以正常工作后,才将其更改为 301(永久)重定向。 301 由浏览器持久缓存,因此可能会使测试出现问题。

总结

您的.htaccess 文件应如下所示:

RewriteEngine On

# Query string...
# Redirect URLs of the form "/index.php?/my-seo-friendly-uri"
# And "/?/my-seo-friendly-uri"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(/.*)
RewriteRule ^(index\.php)?$ %1 [QSD,R=302,L]

# Path-Info...
# Redirect URLs of the form "/index.php/my-seo-friendly-uri"
# Also handles "/index.php" only
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index.php(?:/(.*))?$ /$1 [R=302,L]

# CodeIgniter Front-controller
# (NB: Using query string method to pass the URL)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [L]

补充说明...

  • &lt;IfModule&gt; 包装不是必需的。
  • (.*)^(.*)$ 相同,因为正则表达式默认是贪婪的。
  • 我已将您现有的路径信息重定向(即/index.php/foo)修改为仅重定向/index.php 的请求。这现在需要一个额外的条件来避免重定向循环。
  • 您的 CodeIgniter 前端控制器正在使用查询字符串方法将 /my-seo-friendly-url 传递给后端(如问题中所用)。但是,您设置了$config['uri_protocol'] = 'REQUEST_URI'; - 这与此相矛盾(尽管不一定是问题)。但是,如果您使用的是 REQUEST_URI 方法,那么您可以从最终的 RewriteRule substitution 字符串的末尾删除 ?/$1 部分。例如:

    例如,从这个:

    RewriteRule (.*) index.php?/$1 [L]
    

    到这里:

    RewriteRule . index.php [L]
    

【讨论】:

  • 谢谢@MrWhite,它正在像这样重定向https://www.website.com/index.php?/my-seo-friendly-uri -> https://www.website.com/my-seo-friendly-uri?/my-seo-friendly-uri
  • 对不起,您需要 RewriteRule 上的 QSD 标志来丢弃目标 URL 中的原始查询字符串。我已经更新了我的答案。
  • 现在它适用于index.php?,但不适用于index.php
  • "but not for index.php" - 你的意思是 /index.php 本身并且没有路径信息吗?我已经修改了您现有的重定向来处理这种情况。除了您在问题中发布的指令(已经重定向 /index.php/foo 形式的 URL)之外,我在上面发布的内容应该是所有必需的。我假设您的 .htaccess 文件中没有其他可能会发生冲突的指令。我已经更新了我的答案,其中包含您的 .htaccess 文件应该是什么样子的完整摘要。
  • 太棒了,上次更新成功了。它现在对两者都有效。我没有适当注意识别这两种情况。我想我需要学习很多。此外,这是一个很好的深入解释。再次,非常感谢你
【解决方案2】:

使用 .htaccess ......

 <IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d

 <IfModule mod_php5.c>
     RewriteRule ^(.*)$ index.php/$1 [L]
 </IfModule>

 <IfModule !mod_php5.c>
     RewriteRule ^(.*)$ index.php?/$1 [L]
 </IfModule>

</IfModule>

【讨论】:

  • index.php?工作但不为index.php工作
  • @sanjayojha 上面的代码不会“为index.php? 工作”(即它不会规范化/index.php?/my-seo-friendly-url 的请求)——它根本不包含重定向?!
  • 对不起,但这并不能回答问题。它不会尝试从请求的 URL 中删除 index.php - 以便规范化错误索引的 URL。这里的代码只是一个 CodeIgniter 前端控制器(与问题中已经出现的代码几乎相同)。
猜你喜欢
  • 2015-03-19
  • 1970-01-01
  • 2019-11-23
  • 2016-07-02
  • 2018-03-31
  • 1970-01-01
  • 2013-05-20
  • 2014-09-20
  • 1970-01-01
相关资源
最近更新 更多