.htaccess: rewrite to the folder, unless the cookie exists or before the date bypassing WordPress rewrite

These days, I pretty often port a static site to a WordPress-based site. Once I have finished developing a WordPress site on a test server, I want to install it in the current domain of the website, but without affecting the old static site, until the site appears live.

My idea is to move the current website without WordPress to the /old/ folder and get all requests to rewrite to this folder (and therefore for SEO purposes, keeping all the URLs the same).

To allow myself and other authorized users through I want to check the cookie (which will be set via a simple "php file" login, because I can not rely on static IP addresses), or if the current date and time is after the current date, then let the rewrite rules WordPress takes its course to provide access to the WordPress website.

It will also allow me to add a simple countdown timer on the current or old site, after which the new site will be automatically transferred to a live date.

My simple login.php script looks like this:

 <?php setcookie("login", "loggedin", time()+3600,"/"); ?> <h3>Welcome, you are logged in</h3> <p><a href="/">Click here to visit the home page</a></p> 

I thought the best way to achieve this could be as follows, but it doesn’t work (I get an internal server error). The first section checks if there is a cookie for login, and if not, and the current date and time until 20:00 on July 20, 2012 will rewrite all requests in the /old/ folder. I changed the rewriting section of WordPress to exclude the /old/ folder.

 RewriteCond %{HTTP_COOKIE} !^.*login.*$ [NC] RewriteCond %{TIME} < 20120720200000 RewriteCond %{REQUEST_URI} !^/old/ RewriteRule (.*) http://domain.com/old/$1 [L] # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_URI} !^/(old/.*)$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress 

Can someone help me with what I want to achieve?

+4
source share
2 answers

The RewriteCond directive takes 2 arguments, but you give it 3 when comparing %{TIME} and make apache return a 500 server error. If you look at lexicographic comparisons in RewriteCond , there can be no space after the comparison (mod_rewrite seems to be so stupid). So the line should look like this:

 # no space here -----V RewriteCond %{TIME} <20120720200000 

This should concern a 500 server error.

Another thing about your rules is that the target value of the http://domain.com/old/$1 rule will redirect apache to 302, since you include http://domain.com in the target. If you want it to rewrite internally (so the URL in the address bar of the browser does not change), you need to delete this bit.

+3
source

The easiest way would be to install a plugin called Maintenance Mode

Then create 503.php for the theme.

add this to the top of the file:

 <?php if (!is_user_logged_in()) { header("Location: http://example.com/old"); } else { //WHATEVER YOU LIKE }?> 

This way, registered users can browse the WP website, while others will be redirected to the / old website.

users must log in before @ http://example.com/wp-login.php

0
source

All Articles