代码点火器 2 以及如何实现"down for maintenance"模式?



我知道这个问题可能已经被问过几次了,但我需要一个针对 CodeIgniter 的特定解决方案,使用 .htaccess 文件将每个请求发送到index_failsafe.php文件而不是普通索引.php但前提是 url 不以"admin"开头。例:

  1. www.myhost.com/admin ->照常工作
  2. www.myhost.com/welcome -> 发送到故障保护页面

在案例 1 中:

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

在案例 2 中:

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

我的重写条件是:

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

可以这样做吗?

就我个人而言,我是通过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]

编辑:

如果您使用 cookie 为用户存储会话数据,那么更改 cookie 名称以强制所有人注销可能更简单,然后更改登录页面控制器以加载显示"停机维护"或其他内容的视图。

完成后,只需将 cookie 名称更改回原来的名称,每个人都仍将登录,并确保更改回登录页面控制器加载的视图,以便用户可以正常登录。

若要更改 CI 的会话 cookie,请打开 config.php 并更改以下各项的值:

    $config['sess_cookie_name']

您可以通过创建备用登录控制器并查看标题为"维护登录"或类似内容来更进一步,然后您仍然可以登录进行测试。

这是我需要关闭 SaaS 进行维护时使用的方法,效果很好。我们面向公众的销售页面不受影响,我不必惹htaccess。

最新更新