… or how I set my sites up to go black tomorrow in protest of SOPA and PIPA. You might not see this prior to that blackout, but it has useful applications for other things—essentially any time you want to do a redirect that has a specific schedule.
If you are running your sites on an Apache server with the rewrite module, scheduling a temporary redirect to a maintenance page is pretty simple:
# MAINTENANCE REDIRECT
RewriteEngine on
RewriteCond %{TIME} >20120118000001 [NC]
RewriteCond %{TIME} <20120118235959 [NC]
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule ^(.*)$ /maintenance.html [R=307,L]
To explain the code:
- Line 2:
RewriteEngine on
- Turns on the RewriteEngine, if it isn’t already.
- Line 3:
RewriteCond %{TIME} >20120118000001 [NC]
- Checks to see if the date is greater than a specific time. In this case 2012-01-18 00:00:01.
- Line 4:
RewriteCond %{TIME} <20120118235959 [NC]
- Checks to see if the date is less than a specific time. In this case 2012-01-18 23:59:59.
- Line 5:
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
- Checks to be sure that you’re not trying to access the maintenance page itself, otherwise we’d end up in a redirect loop.
- Line 6:
RewriteRule ^(.*)$ /maintenance.html [R=307,L]
- If all of the above conditions are true, it does a temporary (307) redirect to your maintenance page.
Here’s the code again, with the variables you need to modify italicized.
# MAINTENANCE REDIRECT
RewriteEngine on
RewriteCond %{TIME} >20120118000001 [NC]
RewriteCond %{TIME} <20120118235959 [NC]
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteRule ^(.*)$ /maintenance.html [R=307,L]
Drop it in your .htaccess file, and you should be good to go.
What time value should I use?
We’re talking server time, so your time will be based on what your server setting is. Mine is set to EST, although it gets funky about Daylight Savings Time sometimes, so it might be off by an hour. If you know your server is running 3 hours slower than your timezone, adjust your times by 3 hours.
Then, figure out your date and time in the format YYYY-MM-DD HH:MM:SS
and remove anything that isn’t a number: YYYYMMDDHHMMSS
.
Why are you doing a redirect instead of rewrite?
I like to make it very clear to search engines that we’re on a new page and it is only temporary, so no caching should happen. If a search engine hits my homepage and I’ve rewritten the URI, the search engine thinks that my maintenance content should be indexed, which I definitely do not want to happen. Instead, it knows that there was a redirect.
You want the redirect to be temporary (307) so that the search engines know to continue to request the original URI in the future, and not the maintenance page.
What is this SOPA and PIPA stuff?
The American legislative bodies are both currently considering bills that will allow easy censorship on the Web in the name of protecting copyright and intellectual property. A good analogy is that the bills are kind of like trying to kill a squirrel with an atomic bomb. As someone who is both a Web developer and creative professional, I strongly believe that this bill will hinder creative and economic progress as well as the dissemination of information.
If you’re reading this on Jan 17, or any day after Jan 18, you can learn more on Wikipedia, but it will be blacked out on Jan 18. Of course, your favorite search engine will surely return plenty of hits as well.