Htaccess, %{TIME} and %{REMOTE_ADDR} combined - .htaccess

I would like to set multiple rules based off a set of conditions as follows:
Only apply between 13th April 2016 5am > 11pm.
Only apply if source IP is within range
If both apply, re-direct 2x pages
RewriteCond %{TIME} >20160413050000 [NC]
RewriteCond %{TIME} <20160414230000 [NC]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.10[3-9] [OR]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.11[0-1] [OR]
RewriteCond %{REMOTE_ADDR} ^89\.197\.6\.236
RewriteRule ^confirm.html$ /confirm-logos.html [R=307,L,QSA]
RewriteRule ^blacklist.html$ /blacklist-logos.html [R=307,L,QSA]
The time rule works, the IP range works, but when I have multiple blocks of these they seem to conflict. Is the above correct for what im trying to achieve?

The RewriteCond directive defines a rule condition. One or more
RewriteCond can precede a RewriteRule directive. The following rule is
then only used if both the current state of the URI matches its
pattern, and if these conditions are met.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond
You can use:
RewriteCond %{TIME} >20160413050000 [NC]
RewriteCond %{TIME} <20160414230000 [NC]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.10[3-9] [OR]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.11[0-1] [OR]
RewriteCond %{REMOTE_ADDR} ^89\.197\.6\.236
RewriteRule ^confirm.html$ /confirm-logos.html [R=307,L,QSA]
RewriteCond %{TIME} >20160413050000 [NC]
RewriteCond %{TIME} <20160414230000 [NC]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.10[3-9] [OR]
RewriteCond %{REMOTE_ADDR} ^212\.74\.117\.11[0-1] [OR]
RewriteCond %{REMOTE_ADDR} ^89\.197\.6\.236
RewriteRule ^blacklist.html$ /blacklist-logos.html [R=307,L,QSA]
Or if you have many rules, reversing the test:
RewriteCond %{TIME} <20160413050000 [NC,OR]
RewriteCond %{TIME} >20160414230000 [NC]
RewriteCond %{REMOTE_ADDR} !^212\.74\.117\.10[3-9]
RewriteCond %{REMOTE_ADDR} !^212\.74\.117\.11[0-1]
RewriteCond %{REMOTE_ADDR} !^89\.197\.6\.236
RewriteRule ^ - [L]
RewriteRule ^confirm.html$ /confirm-logos.html [R=307,L,QSA]
RewriteRule ^blacklist.html$ /blacklist-logos.html [R=307,L,QSA]

Related

How to change document root of a website and enforce "https" only?

I am choosing document root via .htaccess in a following manner -
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
How can enforce all requests to https too ?
Try this :
RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
Note clear browser cache the test

.htaccess redirect https://www to https://non-www

i have a 301 redirect from all domains to https://example.de.
The redirect works with http://www.example, www.example with all tld (.com,.eu.net).
But with https://www.example it doesnt redirect to https without the www.
Here is the Mod rewrite:
RewriteEngine on
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^www\.example\.de$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.de$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.at$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.at$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.eu$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.eu$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]
Edit:
The following works so far
RewriteCond %{HTTPS} on [NC]
RewriteCond %{HTTP_HOST} !^example\.de$ [NC]
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteBase /
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]
Your rule doesn't redirect https urls because you are not using correct Rewrite Conditions. Also you dont need to write multiple conditions to test a domain name. Instead of using two RewriteCond lines to test www.example.com and example.com you can do it in a single condition something like. RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ .
The following is the shorter version of your rules.
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.at$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net$ [OR]
# the main domain. redirect www.example.de to example.de
RewriteCond %{HTTP_HOST} ^www\.example\.at$
RewriteRule ^(.*)$ https://example.de/$1 [R=301,L]
Clear your browser cache before testing this change.

.htaccess exclude URI

Have working htaccess, which block specific countries and/or languages,
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^NL$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^BY$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^UA$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RU$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (nl) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (be) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (uk) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (ru) [NC]
RewriteRule ^(.*)$ cap.html [L]
but I need to give access to specific route eg api.html to any country/language.
I try to add exception
RewriteCond %{REQUEST_URI} !^/api\.html
but it does not work, I've tried hundreds of conditions but gave up for 6 hours, please advise what I'm doing wrong.
RewriteEngine On
# Exclude countries, forbidden by law
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^NL$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^BY$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^UA$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RU$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (nl) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (be) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (uk) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (ru) [NC]
RewriteCond %{REQUEST_URI} !^/api\.html
RewriteRule ^(.*)$ cap.html [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ index.php [L]
Try :
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^NL$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^BY$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^UA$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RU$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (nl) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (be) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (uk) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (ru) [NC]
#rewrite everything to "cap.html" except "api.html"
RewriteRule ^((?!api\.html).*)$ cap.html [L]
Your current code seems logical, but you can try by merging the conditions together, and moving the last condition to the top:
RewriteCond %{REQUEST_URI} !^/api\.html [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(?:NL|BY|UA|RU)$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (?:nl|be|uk|ru) [NC]
RewriteRule ^ cap.html [L]
I solved problem in little bit different way
Just created api.php file and added rule to skip existing files
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(?:NL|BY|UA|RU)$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (?:nl|be|uk|ru) [NC]
RewriteRule ^ cap.html [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ index.php [L]

.htacces url rewrite exclude some directory

i have a problem with url rewrite, i have to exclude some directory in whitch there are some file as css,image and other. Howewer i have to
RewriteEngine On
RewriteCond %{REQUEST_URI} !configurazione/ [OR]
RewriteCond %{REQUEST_URI} !funzioni/ [OR]
RewriteCond %{REQUEST_URI} !img/ [OR]
RewriteCond %{REQUEST_URI} !imgprodotti/ [OR]
RewriteCond %{REQUEST_URI} !imgslider/ [OR]
RewriteCond %{REQUEST_URI} !js/ [OR]
RewriteCond %{REQUEST_URI} !palma/ [OR]
RewriteCond %{REQUEST_URI} !sezioni/
RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+).html$ index.php?pag=$1&prodotti=$2&prodotto=$3 [L]
RewriteRule ^([a-z]+)/([a-z]+)/ index.php?pag=$1&prodotti=$2 [L]
RewriteRule ^([a-z]+)/ index.php?pag=$1 [L]
RewriteCond is only applicable to very next RewriteRule. Try this code instead with THE_REQUEST:
RewriteCond %{THE_REQUEST} /(funzioni|img|imgprodotti|js|palma|sezioni|configurazione)/
RewriteRule ^ - [L]
RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+).html$ index.php?pag=$1&prodotti=$2&prodotto=$3 [L,QSA]
RewriteRule ^([a-z]+)/([a-z]+)/ index.php?pag=$1&prodotti=$2 [L,QSA]
RewriteRule ^([a-z]+)/ index.php?pag=$1 [L,QSA]

.htaccess redirect folder only and not redirect the same folder with variables

I'm trying to redirect this
www.mydomain.com/womens/badfolder -> www.mydomain.com/womens/goodfolder
but I do not want to redirect the variables that "badfolder" has.
I would like
www.mydomain.com/womens/badfolder?page=variable
to stay as is. Is this possible?
edit
Here are my rules
RewriteEngine On
RewriteCond %{QUERY_STRING} page=shop.recommend [NC]
RewriteRule . - [F]
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and below that I'll write my redirects.
You can use this rule as your very first rule in your .htaccess just below RewriteEngine On line:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(womens)/badfolder$ /$1/goodfolder [L,NC,R]

Resources