htaccess: allow own domain request and some specific referers - .htaccess

using an .htaccess file i'm trying to allow specific referers and my own domain requests to be processed and deny any others, by redirecting anyway.
CanĀ“t get it work using this .htacces...
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://www\.myowndomain\.com(.*) [NC]
RewriteCond %{HTTP_REFERER} !^http://www\.myfrienddomain\.com/subfolder(.*) [NC]
RewriteRule ^(.*)$ http://www.go-out-of-here.com[R=301,L]
RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
What i try to mean:
If it is not a request from my own domain and it is not a request from my friend's domain...go out of here,
else... passthrough.
Seem to allow from my friends domain but not inner requests, from my own domain.
What's wrong here, friends?? Any ideas?
Thanks in advance!

Related

.htaccess redirect to subfolder, non-www to www and https

So I'm having this website which is stored inside public_html/www/. Some other domains are stored in -for example- public_html/test/. In public_html I have a .htaccess file with a LOT of redirects in it, which all apply to this website inside /www/ folder. Inside the /www/ folder I have another .htaccess (I don't really know why, my colleague did this and he left me with this without information). Since I have no idea how .htaccess exactly works, I have no idea what to do and in which file.
What I need to do is to redirect domain.nl to https://www.domain.nl (some folders should be excluded, like you can see in the code below - emailtemplates, paymentupdate, autocode, nieuwsbrieven, etc.) But domain.nl is stored inside public_html/www/ folder, so it also needs a redirect to the subfolder (which should not be visible in web browser).
At this moment, in the root .htaccess file is the following:
# Use HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/emailtemplates
RewriteCond %{REQUEST_URI} !^/paymentupdate_ideal
RewriteCond %{REQUEST_URI} !^/autocode
RewriteCond %{REQUEST_URI} !^/nieuwsbrieven
RewriteCond %{REQUEST_URI} !^/bevestig-aanmelding-nieuwsbrief
RewriteCond %{REQUEST_URI} !^/bevestig-afspraak
RewriteCond %{REQUEST_URI} !^/images/personal
RewriteCond %{REQUEST_URI} !^/uploadify
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Link to subfolder
# RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
# Non-www to www
#activate rewrite engine
#RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.nl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Probably half of this code isn't even necessary, but I have absolutely no clue. The other .htaccess file, within the subfolder /www/, has the following text which I don't really understand (it contains more, but nothing relevant):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.nl/$1/ [L,R=301]
I'm sorry if I sound stupid. I've tried many things copied from internet, but unfortunately everything has its concequences, which results in other problems with our website (links that don't work anymore, such as). I think the reason it doesn't work all the time, is because it's stored in the subfolder and I don't know how to combine all those lines in one like it would work.
Can anybody help me explain how to fix this and what is going wrong at this moment? Because the website is now not redirecting to www. It's just redirecting to https which gives me a security error at this moment (because the SSL is stored on www.). SUPER thanks in advance!
My problem was wrong order of the checks. First, I redirected to https://, but when I did not use www., it did not redirect to https://www OR it redirected to https://www.www. I fixed my problem by re-ordering my rules, like below:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Permanent redirect to https://
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/excluded_folder1/
RewriteCond %{REQUEST_URI} !^/excluded_folder2/
RewriteCond %{REQUEST_URI} !^/excluded_folder3/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Use subfolder for domain
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]

htaccess redirect not working properly

I am trying to accomplish the following but my code is not working
1) I want to do either non-www to www or www to non-www
2) desktop users should be redirected to example.com/homepage
3) remove or hide /homepage from url
4) redirect mobile users to example.com/m
here is my code, it's not working, too many problems to list, but off the top of my head, I can think of 2 problems that I encountered: "cannot open page because too many redirects occurred" and mobile users are taken to /homepage instead of /m
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
RewriteCond %{HTTP_USER_AGENT} ^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{REQUEST_URI} !^/m/.*
RewriteRule ^.*$ http://example.com/m [R=301,L]
Please help me, I am so frustrated, what am I supposed to change about this code to get it to work?
Unfortunately im no genious with regex or redirects in htacces but I have gotten similar code to work myself.
It does sound like your code is getting stuck in a redirect loop and the browser is stopping it from continuing.
I know thats not a complete answer but Hopefully that helps you make more sense of the code atleast a little.
Hopefully someone else can chime in otherwise i can dig up the code that is working when im on the computer and hopefully give you a better response.
-Sent from my cell
The www/non-www is fine.
For the homepage stuff, you need to do two things: make sure what you're rewriting into the homepage directory actually exists (otherwise you get a 500 server error) and exclude mobile user agents.
So this stuff:
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
Needs to look like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} !^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -d [OR]
RewriteRule ^(.*)$ homepage/$1 [L]

How do I redirect subdomain to subdirectory (with default folder) on htaccess without change address

I'm trying to do a Rewrite subdomains to subdirectories on htaccess.
Searching here I did the rule below:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]
#Rewrite for my Framework
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
When I access the address: http://user.domain.com.br or the address: http://www.user.domain.com.br they're redirected without change the URL address to: http://www.domain.com.br/sd_user/
Well, fine. My problem is:
When someone access a adrees with haven't a folder, .htaccess redirect it to a specific folder. For example:
I haven't a folder called "sd_joecompany", then I want to redirect it to a default folder named "sd_system". If I access http://joecompany.domain.com.br/ and not exists the respective folder, this address may point to "sd_system" keeping the address.
With this code, when I access a subdomain without a respective folder, the following error appear:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /index.php.
Reason: Max-Forwards has reached zero - proxy loop?
I'm not sure about the clarity of my doubt, and I'm sorry for my poor english.
I know a little about .htaccess and these rules. Everithing on the code above, I gathered here!
Thank you since now!
If they are on the same server and have the same document root, then you really don't want to proxy at all. Remove the host part of the rule and get rid of the "P":
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]
#Rewrite for my Framework
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
Cleaned up some of the regex and got rid of the extra RewriteEngine On's.

Htaccess URL masking to specific folder

I have the following problem.
I need when people access to www.mydomain.com they really access to www.mydomain.com/folder, and they keep seeing www.mydomain.com in the URL.
How can this be done with de .htaccess of my server?
Thanks
create a .htaccess file in the root of the site with the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com/$ [NC]
RewriteCond %{REQUEST_URI} !^../folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9/-_.\s]+)?$ ../folder/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?([a-zA-Z0-9/-_.\s]+)$ ../folder/$2 [L]
that's all you need.
RewriteEngine On
RewriteRule ^(.*)$ /folder/$1 [L]
and try to search before you ask something! there are lots of examples like that.

Redirecting from https to http with the exception of some URI's

So the problem I am currently having is that our entire website is indexed using https. we want to redirect to http using .htaccess. the problem is that we need a couple of URIs to still use https and I am not sure how to write the exception for URIs
I know the below example would work if our site functioned like this www.example.com/account.php but our site urls are like www.example.com/index.php?l=account
# Turn SSL on for account
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/account\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
how can i fix this, any guidance would be appreciated.
thanks
EDIT!
So this code below I have working but I would like to make one more exception that I cant seem to get to work, I also want the root (index.php) to only use http.
I tried using...
RewriteCond %{REQUEST_URI} !^/index.php
but this did not work
# invoke rewrite engine
RewriteEngine On
RewriteBase /
RewriteCond %{https} off
RewriteCond %{QUERY_STRING} !^l=product_detail
RewriteCond %{QUERY_STRING} !^l=product_list
RewriteCond %{QUERY_STRING} !^l=page_view
RewriteRule ^(.*)$ https://%{HTTP_HOST}/development/$1 [R=301,L]
thanks
Try
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/account.php
RewriteCond %{QUERY_STRING} !^l=account
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/account.php [OR]
RewriteCond %{QUERY_STRING} ^l=account
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Resources