We have a client that is decommissioning a site by replacing the whole site with a splash page that will direct the users to another site, where they can get the information they’re looking for. Looking around, I found this great article that got me started : http://perishablepress.com/htaccess-redirect-maintenance-page-site-updates/
Here’s the htaccess code as I started with it:
# MAINTENANCE-PAGE REDIRECT <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000 RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC] RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC] RewriteRule .* /maintenance.html [R=302,L] </IfModule>
I’ll break down the important pieces of the code here, for more detail, please visit the source article where I found this:
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
The lines above are IP addresses that you would like the redirection to not work from. So here you would put your IP address if you still want to be able to see the site without redirection. This can be removed if you want everyone including yourself to have the redirection in place.
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
This line above creates the exception to the redirection for the maintenance page you want to redirect to. If you don’t put in the exception, it will create an infinite loop by constantly redirecting to your maintenance page.
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
The line above creates exceptions by file extension. In my case, I’m going to have the maintenance page be an actual splash page with images, javascript, and CSS. If we don’t put in these exceptions, they would redirect also. You can add more extensions as such “(ext1|ext2|ext3|ext4)”.
RewriteRule .* /maintenance.html [R=302,L]
The line above contains where we’re going to redirect to and the type of redirection we’re using. 301 is a permanent redirect while 302 is a temporary redirect. There are other types of redirect codes too: http://en.wikipedia.org/wiki/HTTP_300#3xx_Redirection
For the site I’m working on, I started by pointing directly to the index page on the root with this htaccess code:
# MAINTENANCE-PAGE REDIRECT <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_URI} !/index.php$ [NC] RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC] RewriteRule .* /index.php [R=301,L] </IfModule>
But then I tweaked it a bit and just got rid of the “index.php” to just point to the default document on the root itself with this htaccess code:
# MAINTENANCE-PAGE REDIRECT <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_URI} !/$ [NC] RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC] RewriteRule .* / [R=301,L] </IfModule>
Enjoy!
The post Use htaccess to redirect to root or a maintenance page or splash page appeared first on Local Wisdom.