2012-05-06 88 views
1

我知道這個問題可能已經問了幾次,但我需要一個CodeIgniter的特定解決方案,使用.htaccess文件,它會將每個請求發送到index_failsafe.php文件,而不是普通的index.php,但只有在if該網址不以「管理員」開頭。例如:Codeigniter 2和.htaccess - 如何實施「停機維護」模式?

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

  1. www.myhost.com/admin - - >照常工作
  2. www.myhost.com/welcome>在殼體1發送到故障安全頁面

的情況下,2:

RewriteRule ^.*$ index_failsafe.php/$1 [L] 

我再寫條件是:

RewriteEngine On 

    RewriteBase/
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 

是否可以這樣做?

回答

11

我個人通過IP做到這一點 - 這樣我就可以把我的網站離線,但我還是有着充沛訪問它(測試新的功能,並確保它被帶回之前的工作)

RewriteEngine on 

    # For maintenance: 
    # If your IP address is 1.1.1.1 - then dont re-write 
    RewriteCond %{REMOTE_ADDR} !^1\.1\.1\.1 
    # If the person is requesting the maintenance page, also dont rewrite (prevent loops) 
    RewriteCond %{REQUEST_URI} !/maintenance.html$ 
    # Otherwise rewrite all requests to the maintenance page 
    RewriteRule $ /maintenance.html [R=302,L] 


    # do not rewrite links to the documentation, assets and public files 
    RewriteCond $1 !^(assets) 

    # do not rewrite for php files in the document root, robots.txt or the maintenance page 
    RewriteCond $1 !^([^\..]+\.php|robots\.txt|maintenance\.html) 

    # but rewrite everything else 
    RewriteRule ^(.*)$ index.php/$1 [L] 

只需將!1.1.1.1更改爲您當前的IP。即^ 121.65.56.65

如果不知道你的IP是什麼 - 只是谷歌「什麼是我的IP」 - 這將顯示爲先打

但是在您的具體問題而言 - 這應工作:

RewriteCond %{REQUEST_URI} !/admin$ 
RewriteRule $ /index_failsafe.php [R=302,L] 

編輯:

+0

第二個RewriteCond做什麼? 'RewriteCond%{REQUEST_URI}!/ maintenance.html $' –

+1

我已經編輯了我的答案,以插入評論,但該行基本上說:「嘿,如果他們要求維護頁面,不要重新寫回他們的維護頁面「 - 即防止循環 – Laurence

+0

什麼是'crossdomain.xml'? – Sparky

1

如果您使用Cookie來存儲用戶會話數據,則這可能是簡單的更改cookie名稱以迫使每個人都註銷,然後更改登錄頁面控制器加載一個說「維護」或任何其他的視圖。

完成後,只需將cookie名稱改回原來的樣子,並且每個人都仍然會登錄,並且確保將登錄頁面控制器加載的視圖更改回來,以便用戶可以正常登錄。

要更改CI會話cookie,開拓的config.php並改變值:

$config['sess_cookie_name'] 

你可以把它更進一步通過創建一個替代登錄控制器和查看標題爲「維護登錄」或類似的東西,然後你仍然可以登錄進行測試。

這是我使用的方法,當我需要把我的saas維護下來時,它很好用。我們面向公衆的銷售頁面不受影響,我不必惹惱htaccess。