2010-05-18 129 views
4

我创建了一个CodeIgniter应用程序,现在我试图用index.php将urls重定向到没有它的url。在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网址吗?

谢谢, 加布里埃尔。

+0

我没有得到它。 你想从URL中删除index.php吗? – ronaldosantana 2010-05-18 22:50:17

+0

@Ronaldo Junior是的。 – 2010-05-18 22:53:56

回答

3

最后的规则之前补充一点:

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

或拒绝访问:

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

我试过了,没有改变(有和没有index.php的作品)。 – 2010-05-19 15:48:24

+1

是的,身份证确实,但它应该转发到没有index.php的位置。顺便说一句,因为你使用RewriteBse /,重写规则应该是不同的。我已更新答案以反映 – Artefacto 2010-05-19 18:04:33

+0

使用您的第一个解决方案,我得到: --- 永久移动 文档已移至此处。 此外,尝试使用ErrorDocument处理请求时遇到403禁止错误。 --- 链接被重定向,但内容不起作用。上面的'这里'字有一个链接到同一页面。 --- 我将[R = 301]更改为[L,R = 301](只是尝试解决方案)。它也没有工作。我有一个重定向循环。 --- 我想感谢您的帮助,直到现在。如果您能继续帮助我,我将不胜感激。 – 2010-05-19 19:14:13

0

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

本文介绍如何拿走从CI应用程序的URL“的index.php”。然而,它并没有删除对于CI前端控制器即Index.php的需求,即使Index.php不会出现在URL中,它仍然需要存在于你的站点的顶层(在/ system之上/ 目录)。

+0

我也试过这个,没有任何改变。这两个URL都可以访问。我想让index.php的URL根本无法访问(重定向)。 – 2010-05-19 15:50:01

0

你可以尝试用这个? RewriteRule ^(。*)$ index.php /?$ 1 [L]

希望它能起作用。

+0

不适用所有... – 2010-05-19 18:59:46

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工作)

0

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

走进的application/config/config.php文件并进行以下更改

$config['index_page'] = "";