Redirect all users except me during maintenance - .htaccess

I have this very simple rule in my .htaccess. Basically, i want to redirect all users except me during maintenance. For some reason, it is also redirecting me to maintenance.php.
I don't have any other .htaccess.
The .htaccess is found on My document root: /Users/myname/Sites
# redirect to maintenance page
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
RewriteRule ^(.*)$ /maintenance.php [R=302,L]
I've search and search here and tried pretty much everything for the last 3 days with no success. I cant find what it's wrong with this very simple piece of code? Anyone has any ideas?
If it helps, my access_log says something like:
::1 - - [07/Mar/2021:13:47:36 -0500] "GET /maintenance.php HTTP/1.1" 200 2956

You are redirected your self on /meintanence.php as logfile indicates. Please test this to avoid local request redirecting:
# redirect to maintenance page
RewriteCond %{REMOTE_ADDR} !^::1
RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
RewriteRule ^(.*)$ /maintenance.php [R=302,L]
::1 is the loopback address in IPv6, as the IPv4 version is 127.0.0.1.

Related

.htaccess redirect all request to subfolder only if not specifed ip

I cant figure out how to redirect all requests except my ip to a subfolder in .htaccess
I came up with
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule ^$ /coming-soon/ [L]
RewriteRule (.*) http://example.com/coming-soon [R=301]
But when i write in browser for example example.com/asdasdas it gives me
coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon
What i would like to achieve is when user enters anything that it redirects to example.com/coming-soon unless it is my IP. I did research on SO, but I always get stuck.
Also there is a folder in the root /coming-soon with images and fonts for that html
Any help would be appreciated
You can use this :
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteCond %{REQUEST_URI} !^/coming-soon [NC]
RewriteRule (.*) http://example.com/coming-soon [R=301,L]
Make sure to clear your browser cache or use a different browser to test this code.

Redirect subdomain/subfolder to root domain/subfolder - But only in SOME instances

When I set up my subdomain, I created some links incorrectly. Now Google thinks I have some pages on both my subdomain and my root domain. I need to fix this, but I can't redirect the entire subdomain.
Examples of what I want to do:
https://sub.example.com/ (no redirect)
https://sub.example.com/keep-1 (no redirect)
https://sub.example.com/keep-2 (no redirect)
https://sub.example.com/move-1/* => https://example.com/move-1/*
https://sub.example.com/move-2/* => https://example.com/move-2/*
I've tried a number of .htaccess solutions and I'm close, but I can't figure it out. Here's what I've tried:
Attempt #1 - Correctly redirects, but doesn't work as a solution because it redirects everything from the subdomain
RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301,NE]
Attempt #2 - Doesn't redirect anything - Seems like the right solution but I'm missing something about how redirects work, I think...
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/ [NC]
RewriteRule ^(.*)$ https://example.com/move-1/$1 [L,R=301,NE]
Attempt #3 - Doesn't redirect anything
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/(.*)$ [NC]
RewriteRule https://example.com/move-1/$1 [L,R=301,NE]
Attempt #4 - Doesn't redirect anything
RewriteBase /
RewriteRule ^sub\.example\.com/move-1/(.*)$ https://example.com/move-1/$1 [R=301]
My .htaccess file is in the root domain's root html folder and seems to have control. I have also tried these from the subdomain's root folder, but that didn't redirect anything.
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule ^move(.*) https://example.com/move$1 [R=301,L]
%{HTTP_HOST} is the host name, mapped to domain such as sub.example.com or example.com. It does not contain any path part, that follows behind the domain. $1 is back-reference, mapped to the regex part (.*). The RewriteRule tells if the request uri pattern starts with /move, then redirect to https://example.com/move$1 permanently.

.htaccess not working properly while setting SSL redirects only on certain pages

While looking for an answer for the question above I have encountered this script as a solution(I would like to have my website on http except of a few pages like login, register,manage etc.):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# force https for /login.php and /register.php
RewriteCond %{HTTPS} =off
RewriteRule ^(login|register)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This exact example should work on my server, but it works only partially, meaning, it redirects all pages to http, but I am unable to open login.php or register.php. The webbrowser states that these pages include redirect loop and they can't show up. By no means I am not an expert on mode_rewrite or htaccess so I would appreciate any help.
edit:
I have followed the suggestions and run mywebsite with chrome plugin. I discovered that in the page login.php has redirect loop http->https->http etc. (The only other redirection on that page is when the user is already signed, but it doesn't seem ike a couse for this loop). I have tried also different codes for setting SSL and this one works:
# Rewrite Rules for example.com
RewriteEngine On
RewriteBase /
# Turn SSL on for payments
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/login\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Turn SSL off everything but payments
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/login\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And this one is working correctly, but it has only one exception for https and I would like to have more of them(around 6). Has someone any idea why the first script is not working? or how to modify the second one in order to have more https websites?
Thanks
I had similar problem, this is what worked for me.
In your httpd.conf, under virtual hosts, make sure you have both:
ServerName domain.com
ServerAlias www.domain.com
BOTH in VirtualHost *:80 AND VirtualHost *:443

htaccess RewriteRule causing Error 310 (ERR_TOO_MANY_REDIRECTS)

First I will explain what I am trying to accomplish. I want to only allow access to my website with my own IP Address while I am developing and everyone else will be directed to my offline.html
My website is running on Joomla! 2.5.9
I have added this .htaccess file to the root directory:
I have replaced my IP address with 123.123.123.123 just for putting on here. My IP Address is static.
RewriteEngine On
RewriteCond %{REMOTE_HOST} !^123\.123\.123\.123$
RewriteCond %{REQUEST_URI} !^offline\.html
RewriteCond %{REQUEST_URI} !^(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.swf|\.css|\.js)$
RewriteRule ^(.*) /offline.html [R=307,L]
When I test going to my site through my VPN I will get the below Error message from Chrome:
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.
After that I see the correct path to offline.html has been added and the same with Firefox although the Error Message is slightly different:
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
What could be the cause of this?
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !123\.123\.123\.123
RewriteCond %{REQUEST_URI} !\.(png|jpg|gif|jpeg|bmp|swf|css|js) [NC]
RewriteCond %{REQUEST_URI} !offline\.html
RewriteRule .* offline.html [R=307,L]

htaccess redirect - allow files for construction page

I've tried to set up a htaccess redirect for everyone except me. It works fine...except that I have to write an exception for every file that the under construction page wants. This will take me a while and I'm certain there is a proper way to do it, I just cant find it.
I have tried this:
order deny,allow
deny from all
allow from 205.97.194.334
ErrorDocument 403 http://www.domain.com/page.htm
<Files page.htm>
allow from all
</Files>
But I get an internal server error
What I have now is this:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1
RewriteCond %{REQUEST_URI} !/mypage\.html$
RewriteRule .* http://www.domain.com/construct/mypage.html [R=302,L]
What can I add in this to allow everything in the /construct/ ?#
Thankyou
P.S. Can anyone tell me why the first attempt didn't work?
EDIT:
Ok I've added this, which allowed the files, however, it is only redirecting when the directory is entered. I.e. domain.com will redirect to the construction page, but domain.com/index.php and anything else will not redirect
# Redirect everyone who's not from your IP
RewriteCond %{REMOTE_ADDR} !00.00.00.00 [NC]
# Allow real files to be served
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !index.html$ http://subverb.net/construct/index.html [R=307,L]
If you want /construct to be available to everyone else, and you want them to be redirected to that URL when opening any other URL:
# IF not from your address
RewriteCond %{REMOTE_ADDR} !^123\.4\.5\.6$
# AND not for /construct directory
RewriteCond %{REQUEST_URI} !^/construct
# THEN sen them to /construct/index.html
RewriteRule (.*) /construct/index.html [R=307,L]

Resources