1

I am trying to only show a coming soon or maintenance page to everyone except select IP's. I have tried several solutions now and have settled on this code

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

This works great for whenever someone goes to home page. But if you go to any other page, such as /contact, /shop, .etc it doesn't redirect.

I have placed this code at the bottom of the page. When I tried to move it to the top of the page I received an error stating to many redirects. Below is my standard Wordpress redirect.

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

How do I restrict all pages to all but select ip's?

Thanks

Edit I have tested on a new server without caching plugins or security plugins that add code to htaccess and I still have the same problem. Requests to homepage get redirected but requests to any other page do not. Below is the code from my htaccess file.

# 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
Paul Dowlin
  • 123
  • 2
  • 7

2 Answers2

0

Method #1: Custom code to display a maintenance page

If you want to display a basic maintenance splash page on your site without using a maintenance mode plugin, you can add this bit of code to your 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');

Method #2: Enable maintenance mode through your .htaccess file

For this approach it is necessary to have permission to edit the .htaccess file on your server. This file can be found in the root directory of your website. Once you have this file opened, copy and paste the following code:

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

Method #3: Using WordPress maintenance mode plugins

Govind Soni
  • 320
  • 1
  • 2
  • 11
  • Thanks for answering. I need to allow certain ip's. I don't see how that will work with add_action. Also I have tried your htaccess code and it doesn't redirect when you type in a page (i.e. my.website.com/shop). Have you tried this in a Wordpress environment? – Paul Dowlin Dec 10 '19 at 14:50
  • Yes I have tried and it worked properly at my end, If you're still facing issue then better you should use some Coming Soon Page, Under Construction & Maintenance Mode plugin instead – Govind Soni Dec 11 '19 at 05:48
  • Do you know of any plugins that allow access by ip address without logging in? – Paul Dowlin Dec 18 '19 at 16:29
  • Just thought of this. If I could, what is your stack used to test this? thanks – Paul Dowlin Dec 18 '19 at 16:32
0

Hi, Paul

I tried some solutions base on the question best way to use any plugin for maintenance mode (ex. wp maintenance mode)

or try this

  • You have to create an maintenance.html file in the root directory with any message you want to display to user.
  • Also you can set the ip in the RewriteCond %{REMOTE_ADDR} to set the allowed ip addresses

MAINTENANCE-PAGE REDIRECT

<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>
  • if only want to allow some 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.
  • Thanks but I have already tried your htaccess rules with no success. Have you tried this in Wordpress? Also, while `` works in filter by ip it serves a 403 page. And throws errors when wp-cron.php tries to run. – Paul Dowlin Dec 10 '19 at 14:47
  • Definitely i have tried this in my local setup. also experimented with the LAN systems IPs and it worked for me. – Code_With_Me Dec 16 '19 at 10:10
  • This issue is really bugging me so I'm just asking to be clear. You have a successful redirect on your system whenever you request some page **other** than the home page? Wish I could see this in action as it just doesn't work for me. – Paul Dowlin Dec 18 '19 at 16:27
  • Same as with @GovindSoni. Can I get the stack you are using to test this? thanks – Paul Dowlin Dec 18 '19 at 16:34