【问题标题】:Wordpress .htaccess maintenance mode not workingWordpress .htaccess 维护模式不起作用
【发布时间】:2019-12-10 05:29:45
【问题描述】:

我试图只向除选定 IP 之外的所有人显示即将推出或维护页面。 我现在尝试了几种解决方案并确定了此代码

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^xx\.xx\.xx\.xx
RewriteCond %{REQUEST_URI} !/coming-soon\.html$
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js|woff) [NC]
RewriteRule .* /coming-soon.html [R=302,L]
</IfModule>

当有人访问主页时,这非常有用。但是,如果您转到任何其他页面,例如 /contact、/shop、.etc,它不会重定向。

我已将此代码放在页面底部。当我尝试将其移动到页面顶部时,我收到一条错误消息,指出许多重定向。以下是我的标准 Wordpress 重定向。

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

如何将所有页面限制为除选择 ip 之外的所有页面?

谢谢

编辑 我已经在没有缓存插件或将代码添加到 htaccess 的安全插件的新服务器上进行了测试,但我仍然遇到同样的问题。对主页的请求会被重定向,但对任何其他页面的请求不会。 以下是我的 htaccess 文件中的代码。

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

# END WordPress

# BEGIN Trusted Access
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^xx\.xx\.xxx\.x
RewriteCond %{REQUEST_URI} !/coming-soon\.html$
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js|woff) [NC]
RewriteRule ^(.*)$ /coming-soon.html [R=302,L]
</IfModule>
# END Trusted Access

【问题讨论】:

    标签: wordpress .htaccess maintenance-mode


    【解决方案1】:

    方法#1:自定义代码显示维护页面

    如果你想在你的站点上显示一个基本的维护启动页面而不使用维护模式插件,你可以将这段代码添加到你的functions.php

    function wp_maintenance_mode() {
       if (!current_user_can('edit_themes') || !is_user_logged_in()) {
         wp_die('<h1>Under Maintenance</h1><br />Something ain't right, but we're working on it! Check back later.');
       }
    }
    add_action('get_header', 'wp_maintenance_mode');
    

    方法 #2:通过 .htaccess 文件启用维护模式

    对于这种方法,您必须拥有在您的服务器上编辑 .htaccess 文件的权限。该文件可以在您网站的根目录中找到。打开此文件后,复制并粘贴以下代码:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111
    RewriteCond %{REQUEST_URI} !^/maintenance\.html$
    RewriteRule ^(.*)$ https://example.com/maintenance.html [R=307,L]
    

    方法#3:使用WordPress维护模式插件

    【讨论】:

    • 感谢您的回答。我需要允许某些IP。我看不出这将如何与 add_action 一起使用。我也试过你的 htaccess 代码,当你输入页面时它不会重定向(即 my.website.com/shop)。您在 Wordpress 环境中尝试过吗?
    • 是的,我已经尝试过了,它最终运行良好,如果您仍然遇到问题,那么最好使用一些即将推出的页面,正在建设和维护模式插件
    • 你知道哪些插件不需要登录就可以通过ip地址访问吗?
    • 刚想到这个。如果可以的话,你用什么堆栈来测试这个?谢谢
    【解决方案2】:

    嗨,保罗

    我根据问题尝试了一些解决方案 使用任何插件进行维护模式的最佳方式(例如 wp 维护模式)

    或者试试这个

    • 您必须在根目录中创建一个 maintenance.html 文件,其中包含您想要显示给用户的任何消息。
    • 您也可以在 RewriteCond %{REMOTE_ADDR} 中设置 ip 以设置允许的 ip 地址

    维护页面重定向

    <IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteCond %{REMOTE_ADDR} !^192\.168\.0\.62
     RewriteCond %{REQUEST_URI} !http://192.168.0.254/html/woocommerce-task/maintenance.html$ [NC]
     RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
     RewriteRule .* http://192.168.0.254/html/woocommerce-task/maintenance.html [R=302,L]
    </IfModule>
    
    • 如果只想允许一些ip
    <Limit GET POST>
        Order Deny,Allow
        Deny from all
        Allow from 192.168.0.254
    </Limit>
    
    Note:- Change the ip address with ones that you want to allow and remember to use static ip for development. so it will be easy for you to restrict access for the site.
    

    【讨论】:

    • 谢谢,但我已经尝试过您的 htaccess 规则,但没有成功。你在 Wordpress 中试过吗?此外,虽然&lt;Limit GET POST&gt; 在按 ip 过滤时工作,但它提供 403 页面。并在 wp-cron.php 尝试运行时抛出错误。
    • 我确实在本地设置中尝试过这个。还尝试了 LAN 系统 IP,它对我有用。
    • 这个问题真的很困扰我,所以我只是要求澄清一下。每当您请求某个页面其他而不是主页时,您的系统上就成功重定向了吗?希望我能看到这一点,因为它对我不起作用。
    • 与@GovindSoni 相同。我可以得到你用来测试这个的堆栈吗?谢谢