I need to allow site access to only development team accessing from IPs 111.111.111.111 and 222.222.222.222 and for the rest of the visitors would like to show temporary maintenance page brb.html
I tried this with the following condition and rewrite rule and it got into a redirect loop. Any insights on how to make this work?
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^222\.222\.222\.222
RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.111
RewriteCond %{SCRIPT_FILENAME} !^brb.html
RewriteRule ^.*$ /brb.html [R=307,L]
Note: IPs used above are not the real ones.
You can use:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^(222\.222\.222\.222|111\.111\.111\.111)
RewriteRule !^brb\.html$ /brb.html [R=307,L]
And test in a new browser.
!^brb\.html$ in rewrite rule pattern will stop looping for you.
SOLUTION:
Because of CloudFlare I had to use RewriteCond %{HTTP:X-FORWARDED-FOR} instead of RewriteCond %{REMOTE_ADDR}
I have a EC2 instance and host several sites on the server. I can redirect different domains to their specific folders but I cannot seem to redirect subdomains.
Here is my code so far for www.domain1.com which directs to "folder1" and retains the "www.domain1.com" address in the address bar - This is working fine:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1.com
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule ^(.*)$ folder1/$1 [L]
However I cannot find a solution for sub-domains - I've tried many solutions similar to this - this does not work:
RewriteCond %{HTTP_HOST} sub.domain1.com
RewriteCond %{REQUEST_URI} !^/folder2
RewriteRule ^(.*)$ folder2/$1 [L]
I want http://sub.domain1.com to point to "folder2" but retain sub.domain1.com in the address bar. One thing I want to avoid is using my very long EC2 instance static address "http://ec2-12-34-567-890.compute-1.amazonaws.com/" in the code if this is possible.
Thanks
Try changing the host part to have boundaries:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
and
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
The first rule is matching against the host sub.domain1.com because it contains a domain1.com
I'm relatively new to using .htaccess, and have never done any coding besides what I've read online. I'm using Bluehost, and I'd like to redirect my blog subdirectory to a subdomain. Example: I'd like to redirect www.example.com/blog to blog.example.com.
I already have code in place to always add www. to the beginning of my blog address in the root folder, but I don't know how to accomplish the above redirect by using code in .htaccess. Any help would be appreciated!
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
Edit 2
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
I wanted to add my two cents,
1) to answer the question above, this rewrite should fix it:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^/blog$ http://blog.example.com [R=302,L]
2) but, I think this is not enough by itself, you also need to change DNS, so that blog.example.com is pointed at the right server, and this can be done by a cname similar to this:
blog.example.com CNAME example.com TTL 1080
(not exactly how it will look, but use your DNS webinterface to set this up).
Have you tried this one?
RewriteEngine on
RewriteBase /
RewriteRule ^/blog/(.*)$ http://blog.subdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^check.domain.info$
RewriteCond %{REQUEST_URI} !^/check/
RewriteRule (.*) /check/$1
To redirect subdomain1 and subdomain2 and directory3 to a directory with HTTPS://, I use the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1.example.com [OR]
RewriteCond %{HTTP_HOST} ^subdomain2.example.com [OR]
RewriteCond %{HTTP_HOST} ^example\.com/subdomain3 [NC]
RewriteRule ^(.*)$ https://example.com/subdirectory/$1 [R=301,L]
I'm making significant changes to my website today and want to display a maintenance page for everyone except for me. However, it also keeps redirecting me to the maintenance page even though I whitelisted my IP address (which I triple-checked by doing an ipconfig). I'm guessing something is wrong with my code. Here's my .htaccess file in case anyone can help!
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.playbank\.com$ [NC]
RewriteRule ^(.*)$ http://www.playbank.com/$1 [L,R=301]
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.*
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
192.168.1.* is your LAN network, which is a private network not visible to the rest of the Internet. You need to specify your external IP address, given by your ISP. Enter www.whatismyip.com and use that one.
I have the following htaccess rule I'd like to apply for every IP address apart from mine. I basically want to show a "site is down" page for everyone apart from myself. How is this best achieved?
RewriteEngine on
RewriteCond %{REQUEST_URI} !/indexTEMP.php$
RewriteRule $ /indexTEMP.php [R=307,L]
The Apache variable is REMOTE_ADDR.
Untested but should work:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/indexTEMP.php$
RewriteCond %{REMOTE_ADDR} !^192\.168\.0\.1$
RewriteRule $ /indexTEMP.php [R=307,L]
this applies the rule to every IP except 192.168.0.1.