从CodeIgniter删除并重定向index.php [英] Remove and Redirect index.php from CodeIgniter

查看:45
本文介绍了从CodeIgniter删除并重定向index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got an old project and it has some bizarre issue. Its CodeIgniter project (version 2.X).

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

Google is indexing our URLs with index.php?. Below is an example

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

我可以从url上方看到我的页面,并且还有以下两种变体形式

I can see my pages from above url and with 2 more variants as below

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 .

We have used https://www.website.com/my-seo-friendly-uri throughout the site.

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

My question is how I can redirect https://www.website.com/index.php?/my-seo-friendly-uri and https://www.website.com/index.php/my-seo-friendly-uri to 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 重定向到普通URL,但是 index.php?版本的url仍未重定向

This redirects index.php to normal URLs, but index.php? version of url is still not redirecting

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

In my config.php, I have the following settings

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

推荐答案

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

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

This URL contains a query string and so requires a slightly different rule in order to match it. The RewriteRule pattern matches against the URL-path only (just index.php in this case). The query string is available in its own variable.

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

Add the following, before your existing directives (in addition to the directive that matches /index.php/my-seo-friendly-url - which is passed as 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 )来构造重定向.

The query string is captured (2nd condition), and the backreference (%1) is used to construct the redirect.

为了防止重定向循环,需要先检查 REDIRECT_STATUS 环境变量的条件,因为您似乎在以后的重写中使用查询字符串方法来路由代码点火器URL. REDIRECT_STATUS env var在初始请求中为空,但在首次成功重写后设置为"200"(如200 OK HTTP状态).

The first condition that checks against the REDIRECT_STATUS environment variable is required in order to prevent a redirect loop, since you appear to be using the query string method to route the codeigniter URLs in the later rewrite. The REDIRECT_STATUS env var is empty on the initial request, but set to "200" (as in 200 OK HTTP status) after the first successful rewrite.

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

The QSD flag (Apache 2.4+) is required to discard the original query string from the redirected request. If you are still on Apache 2.2 then append a ? (empty query string) to the substitution string instead. ie. %1?

通过匹配 index.php 可选(即 ^(index \ .php)?$ ),它也会规范省略 index.php ,但仍包含查询字符串的URL(当前可能会或可能不会出现问题).例如./?/my-seo-friendly-uri .

By making the match for index.php optional (ie. ^(index\.php)?$) it will also canonicalise URLs that omit index.php, but still include the query string (that may or may not currently be a problem). eg. /?/my-seo-friendly-uri.

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

Note that this is currently a 302 (temporary) redirect (as is your existing redirect). Only change this to a 301 (permanent) redirect once you have confirmed it works OK. 301s are cached persistently by the browser so can make testing problematic.

您的 .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]

其他说明...

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

  • The <IfModule> wrapper is not required.
  • (.*) is the same as ^(.*)$ since regex is greedy by default.
  • I've modified your existing path-info redirect (ie. /index.php/foo) to also redirect requests for /index.php only. This now requires an additional condition to avoid a redirect loop.
  • Your CodeIgniter front-controller is using the query string method to pass /my-seo-friendly-url to the backend (as used in the question). However, you have set $config['uri_protocol'] = 'REQUEST_URI'; - which contradicts with this (although shouldn't necessarily be a problem). However, if you are using the REQUEST_URI method then you can remove the ?/$1 part from the end of the final RewriteRule substitution string. For example:

例如,从此:

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

对此:

RewriteRule . index.php [L]

这篇关于从CodeIgniter删除并重定向index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆