2013-11-14 29 views
1

我發現了一些關於CodeIgniter和mod-rewrite問題的相關問題,但是,我還沒有找到解決我目前遇到的問題的解決方案。在使用CodeIgniter時正確配置Mod重寫

我目前有:mywebsiteurl.netfirms.com/mywebsite/index.php/admin/login

我想實現從URL中移除的index.php,像這樣:mywebsiteurl。 netfirms.com/mywebsite/admin/login

然而,在嘗試了一些修復後,我已經在這裏StackOverflow上發現經書面here一個可愛的小導遊,我最終跌跌撞撞。

我已經修改了我的MOD-改寫爲如下所示:

<IfModule mod_rewrite.c> 

RewriteEngine On 
RewriteBase/

### Canonicalize codeigniter URLs 

# If your default controller is something other than 
# "welcome" you should probably change this 
RewriteRule ^(home(/index)?|index(\.php)?)/?$/[L,R=301] 
RewriteRule ^(.*)/index/?$ $1 [L,R=301] 

# 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.domain.tld/$1 [L,R=301] 

# Enforce NO www 
#RewriteCond %{HTTP_HOST} ^www [NC] 
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301] 

### 

# Removes access to the system folder by users. 
# Additionally this will allow you to create a System.php controller, 
# previously this would not have been possible. 
# 'system' can be replaced if you have renamed your system folder. 
RewriteCond %{REQUEST_URI} ^system.* 
RewriteRule ^(.*)$ /index.php/$1 [L] 

# 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] 

即使實現這個mod重寫後,我得到一個404如果我嘗試訪問mywebsiteurl.netfirms.com/mywebsite/admin/login,則發生錯誤。如果我去mywebsiteurl.netfirms.com/mywebsite/index.php/admin/login我能夠加載頁面就好了。

我的問題歸結爲如何正確配置我的模塊重寫代碼點火器?也許我錯過了一些設置?我對mod重寫不夠熟悉,不知道哪裏出了問題。

我還應該提到我在Code Igniter配置文件中進行了更改。

$config['index_page'] = ''; 

有沒有人有任何想法,我可能在做什麼錯在這裏?

在此先感謝。

+0

你的htaccess文件在哪裏? –

+0

我有我的htaccess文件裏面的public_html/mywebsite目錄訪問mywebsiteurl.netfirms.com/mywebsite/ – Ryan

回答

1

我有我的htaccess文件在mywebsiteurl.netfirms訪問的public_html/mywebsite目錄中。COM/mywebsite/

你需要更新你的重寫基地和另外一個規則:

RewriteBase/

需求是:

RewriteBase /mywebsite/ 

而此行

RewriteRule ^(home(/index)?|index(\.php)?)/?$/[L,R=301] 

需要是

RewriteRule ^(home(/index)?|index(\.php)?)/?$ /mywebsite/ [L,R=301] 

而這需要有領先的斜線刪除:

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

,使其像:

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

當有規則的目標沒有領先斜線,這是一個相對URI -path並使用該基礎來確定根。由於你的內容是在mywebsite,基地需要反映。

+0

這似乎已經解決了這個問題。非常感謝你。 – Ryan

0

我想我剛剛爲您的問題找到了更好的解決方案,而且非常簡單。 CI中的配置文件,留空index_page和BASE_URL:

$config['base_url'] = ''; 
$config['index_page'] = ''; 

笨將人物親自爲你的基本路徑,並在URI將不使用的index.php段。

然後在您的.htaccess文件,應用指令重定向所有URL,但文件夾和文件名到您的index.php:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [L] 
</IfModule> 

這將做的工作,和你沒有任何改變的任何配置文件,當你改變你的項目根文件夾。

希望有所幫助。