How to Stop Redirect Loop? - .htaccess

I have 2 bits of code that I need help with!
Here is the first, which im trying to redirect the visitors that have no referrer:
RewriteCond %{HTTP_REFERER} ^$
RewriteRule .* http://sitetoredirectto.com [R,L]
and also visitors that type in my url directly (unless its my ip):
RewriteCond %{REMOTE_ADDR} !^28\.473\.38\.251
RewriteRule .* http://sitetoredirectto.com [R,L]
I want to get both of these into the same htaccess file so what do I have to change to get them both to allow only my IP while keeping the functionality they have plus stopping the redirect loop?

You can use it this way:
RewriteCond %{HTTP_REFERER} ^$
RewriteCond %{HTTP_REFERER} !sitetoredirectto\.com [NC]
RewriteCond %{REMOTE_ADDR} !^28\.473\.38\.251
RewriteRule .+ http://sitetoredirectto.com/? [R,L]

Related

htaccess rule to protect some files independent from from protocoll and host

I have an htaccess to limit the access to files to a direct click in the website. Copy the link send to another one and open it should be forbid.
My htaccess works well
RewriteCond %{HTTP_REFERER} !^http://localhost.*$ [NC]
RewriteRule ^.*$ - [NC,R=403,L]
Now I tried to make it more variable to protocol and host, but now nobody can access the files. The code should do the same as above only with variable http(s) and localhost part ...
RewriteCond %{HTTPS} =on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [env=proto:http]
RewriteCond %{HTTP_REFERER} !^%{ENV:proto}://%{HTTP_HOST}.*$ [NC]
RewriteRule ^.*$ - [NC,R=403,L]
I don't see the error. Can someone help to solve this?
If you change variables to manual entries, will it work?
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^(http://|https://)(www.)?(example.com).*$ [NC]
RewriteRule ^.*$ - [NC,R=403,L]

Redirect select pages to https

I have a very simple question that for some reason I cannot figure out, and hours of searching has not helped either. Using an .htaccess file, how can I redirect just /login.php and /index.php to https, and then redirect any other page to just http? I currently use this code to redirect to https, but it redirects every page:
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*) https://www.ruxim.com/$1 [R]
thank you very much.
The %{SERVER_PORT} variable depends on the UseCanonicalPhysicalPort in your config. If it's not setup, then you may not be able to match against that variable, easier to use %{HTTPS} instead.
RewriteCond %{HTTPS} off
RewriteRule ^/?(login|index)\.php https://www.ruxim.com%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule !^/?(login|index)\.php http://www.ruxim.com%{REQUEST_URI} [L,R]
If you don't need the redirect to non-https, then you don't need the second rule.
Try something like this;
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_FILENAME} =index.php [OR]
RewriteCond %{REQUEST_FILENAME} =login.php
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
I'm note 100% sure if the [OR] will comply suddenly in the middle.
Please apply following conditions to secure only /login.php and /index.php pages. Other pages will be work on HTTP path (non-secure pages).
RewriteEngine On
RewriteBase /
# force https for /login.php and /index.php
RewriteCond %{HTTPS} =off
RewriteRule ^(index|login)\.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} !^/(index|login)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

how to redirect using HTTP_REFERER on htaccess

I did my research, but I'm still not able to make this work...
All i'm trying to do is to redirect all incoming traffic from a specific link on another webpage to a page of my choosing...
Options +FollowSymLinks
RewriteCond %{HTTP_REFERER} http://theotherwebsite.qc.ca/ [NC]
RewriteRule ^http://www.cnn.com$ [L]
Why it doesn't work?
RewriteCond needs a regular expression
RewriteCond %{HTTP_REFERER} ^http://theotherwebsite\.qc\.ca/ [NC]
RewriteRule needs a regular expression what is should redirect and the target
RewriteRule ^(.*) http://www.cnn.com [L]

Single referrer traffic .htaccess

RewriteEngine on
RewriteCond %{HTTP_REFERER}!(.*)ocean-leecher.net(.*)
RewriteCond %{HTTP_REFERER}!(.*)blindtext.info(.*)
RewriteCond %{HTTP_REFERER}!(.*)yourdomain.com(.*)
RewriteRule ^(.*)$ - [F]
This code accept 3 referrer traffic to all pages of main domain.
Is there any way to make this code valid for just maindomain.com/thankyoupage.php
Add the following
RewriteCond %{REQUEST_URI} ^/thankyoupage.php$
Change your code to this:
RewriteEngine on
RewriteCond %{HTTP_HOST} maindomain\.com$ [NC]
RewriteCond %{HTTP_REFERER} !(ocean-leecher\.net|blindtext\.info|yourdomain\.com) [NC]
RewriteRule ^thankyoupage\.php$ - [F,NC,L]

Temporary redirect to maintenance page

I am trying to write a rule to redirect all URLs to a temporary page so that some site updation could be done, but it ends up in an infinite loop.
RewriteCond %{HTTP_HOST} ^(.*)mysite\.com$
RewriteCond %{REQUEST_URI} !^(.*)temp$
RewriteRule ^(.*)$ http://www.mysite.com/temp [R=307,L]
How to check if it's a temp page?
You need to write rule for all request except maintenance file.
.htaccess should be:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]
What I do is to redirect all traffic to a maintenance.html page when it's not coming from my IP.
The first rewrite condition avoids an infinite loop.
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REMOTE_ADDR} !^172\.16\.254\.1$
RewriteRule $ /maintenance.html [R=302,L]
It works, I tried it. It will redirect all requests to a maintenance page.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.html [R=302,L]
</IfModule>

Resources