在CodeIgniter中重定向index.php

时间:2010-05-18 22:37:46

标签: php .htaccess codeigniter

我创建了一个CodeIgniter应用程序,现在我正在尝试将带有index.php的网址重定向到没有它的网址。

我目前的.htaccess是:

RewriteEngine On
RewriteBase /

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www
# If you have subdomains, you can add them to 
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ http://www.plugb.com/$1 [L,R=301]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

唯一的问题是,使用和不使用index.php可以访问同一页面。

例如:

http://www.plugb.com/index.php/games/adventure-rpg

http://www.plugb.com/games/adventure-rpg

有没有办法重定向index.php网址?

谢谢你, 加布里埃尔。

5 个答案:

答案 0 :(得分:3)

在最后一条规则之前添加:

RewriteBase /    
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^index.php(/.*)?$ http://%{HTTP_HOST}$1 [R=301]

或拒绝访问:

RewriteRule ^index.php(/.*)?$ - [R=404]

答案 1 :(得分:0)

CodeIgniter wiki documentation描述了如何实现这种确切的行为。

  

本文介绍如何从CI应用程序网址中删除“index.php”。但是,它不会消除对Index.php的需求,这是CI前端控制器,即使Index.php不会出现在URL中,它仍然需要出现在您站点的顶层(在/ system之上) / directory)。

答案 2 :(得分:0)

你能尝试一下吗? RewriteRule ^(。*)$ index.php /?$ 1 [L]

希望它会起作用。

答案 3 :(得分:0)

我想出了一个解决方法:

$clean_url = str_replace('index.php','',current_url());
$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

if($clean_url!==$current_url)
{
    redirect($clean_url,'location',301);
}

不是.htaccess,但是在你的控制器中可以解决这个问题。 (至少,它在PlugB

中有效

答案 4 :(得分:0)

您不需要编写代码来'重定向'index.php URLS。

进入application / config / config.php并进行以下更改

$config['index_page'] = "";