Redirecting with .htaccess

Posted by on May 12, 2010 in .htaccess | 0 comments

Using the following .htaccess code, you can redirect your users to another destination. In this example we are redirecting the visitors to a "Under Construction" page (construction.php).

Make sure you upload your .htaccess files in ascii mode, sending it up as binary will break it and usually make your server unhappy and don’t forget that .htaccess does not work if you’re on a windows server. Also, make sure to change “domain.com” in this example with your own website URL and you’ll be all set.

The code below blocks all visitors from accessing any page besides the "construction.php" page.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/construction\.php$
RewriteRule ^(.*)$ http://domain.com/construction.php[R=307,L]

The below code sends all users to construction.php except those with the specified IP. If you need to test your website while others were locked out, then you can use the below code with your IP address. When you are done with maintaining your website you can just remove the .htaccess code and the site will be back up again immediately!

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
RewriteCond %{REQUEST_URI} !^/construction\.php$
RewriteRule ^(.*)$ http://domain.com/construction.php [R=307,L]

By the way, you can use this website to figure out what your IP address is: http://www.ipchicken.com/

Add a Comment
no comments