Boolean AND in .htaccess - .htaccess

Well I am using the following code to redirect users who don't have my IP to a coming soon page:
rewritebase /
rewritecond %{REMOTE_HOST} !(^1\.2\.3\.4)
rewritecond %{REQUEST_URI} !/comingsoon\.html$
rewriterule .* http://www.andrew-g-johnson.com/comingsoon.html [R=302,L]
I want to make it so that I have two IP's that are allowed, any ideas how?

I'd actually recommend Cletus' suggestion, but an alternative if you wanted to stick with .htaccess would also be to just add on more lines of conditions (benefit is it's more legible than concatenating them all into one long regex):
rewritebase /
rewritecond %{REMOTE_HOST} !(^1\.2\.3\.4)
rewritecond %{REMOTE_HOST} !(^5\.6\.7\.8)
# and so forth
rewritecond %{REQUEST_URI} !/comingsoon\.html$
rewriterule .* http://www.andrew-g-johnson.com/comingsoon.html [R=302,L]

You could specify a regular expression like:
(1\.2\.3\.4|5\.6\.7\.8)
but that becomes unwieldy the more exceptions you want.
what you might want to do instead use mod_access (assuming you're using Apache) with allow and deny directives.
order deny, allow
deny all
allow from 1.2.3.4
allow from 5.6.7.8
ErrorDocument 403 http://www.andrew-g-johnson.com/comingsoon.html
combined with a custom error document for 403 that says coming soon.

RewriteCond directives are combined implicitly with AND. So you just have to add another RewriteCond directive to express this:
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4$
RewriteCond %{REMOTE_HOST} !=5.6.7.8
RewriteRule !^comingsoon\.html$ comingsoon.html [R=302,L]
Additionally, you should specify the begin an the end of the address usind ^ and $ or do a lexicographical comparison with =.

Related

Using two RewriteRule in .htaccess

I have two RewriteRule in my .htaccess file which is hosted at godaddy.First one is for redirecting http to https and works fine.Second one is for using slash instead of question marks in URL but not working.Where am I wrong?
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sosyosapien.com/$1 [R,L]
RewriteBase /
RewriteRule ^low/$ https://www.sosyosapien.com/index.php?postt=$1&konu=$2&kategori=$3 [QSA,NC,L]
Edit:To be more clear,my URL now looks like :
sosyosapien.com/index.php?postt=23&konu=what_is_sosyosapien&kategori=science
But I want it to look like
sosyosapien.com/index.php/23/what_is_sosyosapien/science
But my code only does http to https redirect and cant change the URL in the way I want.Sorry I dont have enough knowledge of .htaccess' working principle.Can you please give me a code to do what I need?
You most likely do not want to add this RewriteRule to your .htaccess. It is incorrect and it is missing capturing groups that it has been referred to:
RewriteBase /
RewriteRule ^low/$ https://www.sosyosapien.com/index.php?postt=$1&konu=$2&kategori=$3 [QSA,NC,L]
If you really have to keep this, you might find out what $1, $2 and $3 are that you might want to redirect. Then, you could change this ^low/$ based on that.
Maybe, just add this, and it might work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sosyosapien\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sosyosapien.com/$1 [R,L]
RewriteCond %{QUERY_STRING} postt=(.*)
RewriteRule ^(.*)\/index\.php\?postt=([0-9]+)&konu=(\w+)&kategori=(\w+)$ ^$1\/$2\/$3\/$4$ [R,L,302]
</IfModule>
This RegEx might help you to simplify your expression, as you wish, since I'm not sure what types of conditions you might have:
^(.*)\/index\.php\?postt=([0-9]+)&konu=(\w+)&kategori=(\w+)$
Graph
This graph shows how the RegEx works:
You might need to restart apache:
sudo apachectl restart
You also want to clear your browser cache, every time that you would make a change in .htaccess.

.htaccess Force SSL (HTTPS) On all pages besides a few

Ok, so i need to forward all traffic through HTTPS besides on ONE specific page. Here is my current .htaccess code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
ErrorDocument 404 /404.html
Options -Indexes
So basically i need to forward all traffic through HTTPS BESIDES my /r.php & /l.php & /c.php page, can this be done? I tried doing some research but haven't found too much.
EDIT:
Would this work?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/r.php$
RewriteCond %{REQUEST_URI} !^/c.php$
RewriteCond %{REQUEST_URI} !^/l.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
IT IS HIGHLY RECOMMENDED TO USE A SECURE CONNECTION IF IT IS AVAILABLE TO YOU
RewriteEngine On
RewriteCond %{HTTPS} off
#Add slash(es) before special characters to escape them as literals
RewriteCond %{REQUEST_URI} !^\/r\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
What this does is check if your HTTPS flag is set on the server, if it is not set, and the page is not /r.php then redirects the page to the secure HTTPS equivalant.
EDIT: The /r.php entity needs to have the / and . escaped by having these characters preceeded by backslashes \.
The RewriteCond line "escapes" the /r.php page reference from the Secure connection flag check, it is better to use the proper server flag as detailed here, rather than manual port requests because secure/insecure standard hypertext protocol ports can be ANY port on a server, and it is only convention (and not at all required) that TLS/HTTP ports are 443/80 etc.
You can add a new RewriteRule above your RewriteCond telling apache to process it as is and not applying any more RewriteRules (the L says, stop after matching this rule, (r|c|l) is a regular expression for matching r OR c OR l).
RewriteRule ^/?(r|c|l)\.php$ - [L]
See https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule for more information.

Allow access when certain query string exists otherwise deny

I want to allow access to my site when there is s=stick_path or s=stick_relative in the query string otherwise deny the access. e.g allow access when www.domain.com/?s=stick_path but deny access when www.domain.com/?s=ck_relative or www.domain.com/?s=action_path.
I have tried for simple example like www.domain.com/?s=stick_path but it does not seem to work.
RewriteCond %{QUERY_STRING} !^s=stick_path$
RewriteRule ^.* - [F,L]
I think one can trick the .htaccess rule if s parameter is present twice like www.domain.com/?s=stick_path&s=action_path
The rules you have work for me when I try to go to http://localhost/?s=stick_path, but if you want to be able to check anywhere for the query string, try:
RewriteCond %{QUERY_STRING} !(^|&)s=stick_path(&|$)
RewriteCond %{QUERY_STRING} !(^|&)s=stick_relative(&|$)
RewriteRule ^.* - [F,L]
This works if either s=stick_path or s=stick_relative is anywhere in the querystring.
Answer pulled from an OP's update
RewriteEngine ON was missing in the .htaccess.
So, the correct code was:
RewriteEngine on
RewriteCond %{QUERY_STRING} !^s=stick_path$
RewriteRule ^.* - [F,L]

Deny access to all files but index.php but allow GET variables

Basically I do not want people that visit my site to get all of the files, but all the things I tried and found on the internet disallow the usage of GET variables after the index.php. I'm using a rewrite to make domain.com/lol go to index.php?lol.
This is my current .htaccess file, if you'd like to modify it to make it easier for me, go ahead too.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [L,R=301]
RewriteRule ^act/(.*)$ index.php?act=$1
RewriteRule ^code/(.*)$ index.php?code=$1
RewriteRule ^login$ index.php?login
RewriteRule ^logout$ index.php?logout
RewriteRule ^add$ index.php?add
RewriteRule ^tac$ index.php?tac
RewriteRule ^profile$ index.php?profile
Following rule stops direct requests to index.php (either with or without) arguments:
# block direct requests to index.php and redirect it to /
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index.php$ /
If needed, you can change the rewrite target and/or add some more conditions based on what exactly is allowed and what's not.

.htaccess redirect to all IP's but mine

Basically, I am trying to work on the front end of a website, but I would like everyone else but myself to be redirected to a construction page if you like. I currently have:
redirect 301 /index.php http://www.domain.com/construction.php
While this works, it works to well, I would like to be able to still see the live site myself, is it possible to exclude everyone but my IP?
Thanks again.
You could do it with mod_rewrite
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule index.php$ /construction.php [R=301,L]
You'll need some conditions before redirecting:
RewriteCond %{REMOTE_ADDR} !=1.3.3.7
RewriteCond %{REQUEST_URI} !=/construction.php
RewriteRule .* /construction.php [L]
Also, to make sure after the lock-out is removed, clients will see the actual page, this solution does not redirect clients permanently (using a 301 redirect), but internally redirects. Substitute 1.3.3.7 for the actual IP address you're using.
If your apache version is 2.4* , You can redirect your visiters to construction page using the following directives in htaccess :
<If "%{REMOTE_ADDR} !='yourIp'">
RedirectMatch ^/((?!construction.php).*)$ /construction.php
</If>
It says if the ip address is not yourIp redirect all requests to /construction.php .
On older versions of apache, you can use the following mod-rewrite based solution :
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^myIP$
RewriteRule !construction\.php /construction.php [L]
This internally forwords the request to /construction.php if the RewriteCondition meets. You can Replace L with R if you want to see the redirected url in browser address bar.
hi there you could do the following in .htaccess file
RewriteEngine on
# Redirect all except allowed IP
RewriteCond %{REMOTE_ADDR} !^12.345\.678\.901$
RewriteRule /index.php http://www.domain.com/construction.php [R=302,L]
putting your IP instead of 12.345.678.901
If you have a range of IPs you want to exclude from seeing 'under construction' page you can use |
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^127.0.0.1|212.250.141.228
RewriteRule ! construction\.html /construction.html [R]
It is important to put the 2 last lines at the end of your .htaccess file, especially when it contains more rewriting rules.
The following worked for me
Deny from all
Allow from xxx.xxx.xx.xxx
If you are interested on having a background image referenced on your construction.php, the code below avoids the image to be redirected:
RewriteCond %{REMOTE_ADDR} !=THE_IP
RewriteCond %{REQUEST_URI} !^\/construction\.php|\/YOUR_IMAGE\.jpg
RewriteRule .* /construction.php [R=302,L]
In addition to using the if directive as other answers suggested, you can also add multiple IPs by including other conditions into one directive using the && operator as such:
<If "%{REMOTE_ADDR} != '127.0.0.1' && %{REMOTE_ADDR} != '192.168.1.1'">
RedirectMatch ^/((?!construction.php).*)$ /construction.php
</If>
See the docs here: http://httpd.apache.org/docs/2.4/mod/core.html#if
Another idea is to give access only to a certain range
RewriteEngine on
RewriteBase /
# Validator
SetEnvIf Remote_Addr "^128.30." IsInt
# Local
SetEnvIf Remote_Addr "^192\.168" IsInt
Order allow,deny
Allow from env=IsInt
Not any one worked until I find my own solution
URL in code: http://www.example.com/index_cons.php
IP address in example is: 75.85.95.105
Tested on lastest version of Cpanel.
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^75\.85\.95\.105
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/index_cons\.php" [R=302,L]

Resources