htaccess Allow only internal IP - html

RewriteCond %{REMOTE_ADDR} !=173.220.70.101 [OR,NC]
RewriteCond %{REMOTE_ADDR} !=173.220.70.102
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
I would like to have a or statement for two IP address
the old version worked for one IP
not sure why this isn't working anymore.

Use:
RewriteCond %{REMOTE_ADDR} !=173.220.70.101
RewriteCond %{REMOTE_ADDR} !=173.220.70.102
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Without [OR]...

Allowing only 173.220.70.1 and 173.220.70.2
RewriteCond %{REMOTE_ADDR} !^173.220.70.[1-2]$
RewriteRule ^.* - [F]
Allowing any IP except 173.220.70.1 and 173.220.70.2
RewriteCond %{REMOTE_ADDR} ^173.220.70.[1-2]$
RewriteRule ^.* - [F]

Related

Combining HTAccess rules into one

Hey everyone im trying to use Htaccess to redirect all pages to https:// while keeping the query string without fbclid if it's there.
IE.
https://www.example.com/?sdfuh
http://www.example.com/?sdfuh
www.example.com/?sdfuh
https://www.example.com/?sdfuh&fbclid=azduh
http://www.example.com/?sdfuh&fbclid=azduh
www.example.com/?sdfuh&fbclid=azduh
example.com/?sdfuh&fbclid=azduh
They should return https://example.com/?sdfuh
Ok so after some help Ive got to this
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [AND]
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [AND]
RewriteCond %{QUERY_STRING} ^(.*)
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
RewriteCond ^(www.)?example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/ [R=301,L]
This works with:
https://www.example.com/?sdfuh
http://www.example.com/?sdfuh
www.example.com/?sdfuh
https://www.example.com/?sdfuh&fbclid=azduh
http://www.example.com/?sdfuh&fbclid=azduh
Not with
www.example.com/?sdfuh&fbclid=azduh
example.com/?sdfuh&fbclid=azduh
I need to figure out how to make it shorter and work with all the possibilities
Rather than literally combining the rules, I believe the main piece of knowledge you are missing out on is the use of [OR] - which means a RewriteRule won't fail if only one of the RewriteCond results in true.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1%2 [R=301,L]
You can test this out here: https://htaccess.madewithlove.be?share=f4381086-80b4-52b6-883e-8921f5de2316
After alot of help this solved my issue of stripping facebooks tracker, keeping my own query string and confirming https:// is used.
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)fbclid=(?:[^&]*)((?:&|$).*)$ [NC]
RewriteRule ^(.*)/?(.*)$ https://example.com/?%1%2 [R,N]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)/?(.*)$ https://example.com/$2 [R,L]

how to redirect on single type of url

I want to redirect on single domain URL on like this https://www.example.com/
when user will type any one of belove URL
https://example.com/ or
www.example.com or
http://example.com
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^index$ "https\:\/\/www\.example\.com\/" [R=301,L]
this works for me
This is what you need.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com\/
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

How to add HTTPS via .htaccess file

I have installed an SSL on my website and I have a .htaccess file that has some conditions already written. I have looked at a number of methods shown online on how to force https. However, none of them seem to work. I keep having broken links.
The code I am currently using, the rule commented out does not work for https
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#This condition doesn't seem to work
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect non www. to www.
#RewriteCond %{HTTP_HOST} !^www\.MYWEBSITE\.co\.uk
#RewriteRule (.*) http://www.MYWEBSITE.co.uk/$1 [R=301,L]
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1? [R=301,L]
# Remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^(.+)\.php /$1 [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
If you would like to force HTTPS you can use the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Htaccess file redirects wrong ip's

I'm trying to redirect spam ip's from my website to a different website.
For some reason the following code in my .htaccess file redirects me (my ip) to the www.underconstruction.com despite my ip not being included. My ip has the following format 5.64...
Please, could you help me improve the following code so that it redirects only the listed ip's / ip ranges to www.underconstruction.com
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^88\.208\.201\.208 [OR]
RewriteCond %{REMOTE_ADDR} ^209\.190 [OR]
RewriteCond %{REMOTE_ADDR} ^209\.51 [OR]
RewriteCond %{REMOTE_ADDR} ^5\.101 [OR]
RewriteCond %{REMOTE_ADDR} ^62\.210 [OR]
RewriteCond %{REMOTE_ADDR} ^62\.212
RewriteRule ^/* http://www.underconstruction.com/ [L]
Have you checked your error log? Try to use REMOTE_HOST instead of REMOTE_ADDR
The problem can be solved by using the following code in the .htaccess file. The REMOTE_HOST parameter has been used instead of the REMOTE_ADDR
RewriteEngine On
RewriteCond %{REMOTE_HOST} ^88\.208\.201\.208 [OR]
RewriteCond %{REMOTE_HOST} ^209\.190 [OR]
RewriteCond %{REMOTE_HOST} ^209\.51 [OR]
RewriteCond %{REMOTE_HOST} ^5\.101 [OR]
RewriteCond %{REMOTE_HOST} ^62\.210 [OR]
RewriteCond %{REMOTE_HOST} ^62\.212
RewriteRule ^/* http://www.underconstruction.com/ [L]

Exclude all Files from Rewrite (NGINX to APACHE Migration)

My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The Redirect and Rewrite were working perfect until recently I switched from NGINX back to APACHE.The trouble is that it is now also rewriting file extensions which I don't want!How can I only apply it only to directories and exclude ALL files from it?
Figured it out! :)
Actually had to add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
before the first rewrite!
So the htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]