2010-05-18 31 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'] = "";