.htaccess - Prevent direct subdomain access - .htaccess

I've got a subdomain which i need to disable access too(give a 404).
Example; [abcd.domain.com] - when this url is accessed, the user should receive a 404Page not found.
I have managed to get this working with the code below, BUT, however, its also giving me a 404Page not found EVEN when i browse to [abcd.com]
How could this be modified so it would work both ways?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abcd.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.abcd.domain.com$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ - [L,R=404]
Thank you very much!!

You have an extra [OR] in your conditions.
Simplified rule should be:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abcd\.domain\.com$ [NC]
RewriteRule ^/?$ - [L,R=404]

Related

Force user to a single site when visiting over a specific domain via htaccess

I try to force a user which is coming via a specific domain example-page.com to always get redirected to /countdown
My approach was the the following:
RewriteCond %{HTTP_HOST} ^example-page\.com [NC, OR]
RewriteCond %{HTTP_HOST} ^www.example-page\.com [NC]
RewriteCond %{REQUEST_URI} !^/countdown
RewriteRule ^(.*)$ https://www.example-page.com/countdown [L,R=301]
But for some reason I always get a "Too many redirects error".
I get redirected to the https://www.example-page.com/countdown but somehow the RewriteCond is triggered again but it should not because of the RewriteCond %{REQUEST_URI} !^/countdown.
You may try this rule:
RewriteCond %{HTTP_HOST} ^(?:www\.)?example-page\.com [NC]
RewriteCond %{THE_REQUEST} !\s/countdown [NC]
RewriteRule ^ https://www.example-page.com/countdown [L,R=301]
Make sure to test if after clearing your browser cache.
Could you please try following, with your shown samples. Please keep these Rules at top of your htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example-page\.com [NC]
RewriteCond %{REQUEST_URI} !^/countdown
RewriteRule ^ https://www.example-page.com/countdown [L,R=301]
Also why you are getting error because you have space between NC and OR(NC, OR) which is causing that 500 internal error.

forbidden empty user agent, but allow it for some specific domain, by using htaccess

I want to block the empty user agent, but in the scripts of the website, there are some snippets use the file_get_contents function, which will be blocked as well. I tried to add header to the file_get_contents, but it was not good. So I tried to use htaccess to bypass the localhost.
Here is the rule in htaccess:
#Block empty User-Agents
RewriteCond %{HTTP_USER_AGENT} ^-?$
RewriteCond %{HTTP_HOST} !^http://(.*\.)?example\.com [NC]
RewriteRule ^ - [F]
But it does not wok. How should I change? Thanks in advance.
After edit and test for several times, I make it worked:
#Block empty User-Agents
RewriteCond %{HTTP_USER_AGENT} ^-?$ [NC]
RewriteCond %{HTTP_HOST} !^http://(.*\.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/url [NC]
RewriteRule ^ - [F]
Also tried:
#RewriteCond %{HTTP_HOST} !^(127\.0\.0\.1|localhost) [NC]
#RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
Doesnot help. Only the REQUEST_URI works.

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]

REQUEST_URI negation not working on redirection

I am trying to setup my website to redirect to mobile version using .htaccess.
My main intention is to redirect all requests coming from mobile except requests seeking image files directly. Image files are located in three folders under the root folder: /img/ /files/ /userfiles/
I am using the below:
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipad|ipod|opera mobile|palmos|webos)" [NC]
RewriteCond %{REQUEST_URI} !^/files/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/img/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/userfiles/(.*) [NC]
RewriteRule ^(.*)$ http://mobilesite.com/$1 [L,R=302]
Redirection is taking place for all requests coming from mobile devices without checking the folder conditions.
If I try to access www.normalsite.com/img/somefile.jpg, it is still redirecting me to mobilesite.com even though the condition above should prevent it from doing so.
Can somebody please help?
Thank you.
Should be something like this:
# Assume this condition works.
RewriteCond %{HTTP_USER_AGENT} "(android|blackberry|googlebot-mobile|iemobile|iphone|ipad|ipod|opera mobile|palmos|webos)" [NC]
# Modified conditions
RewriteCond %{REQUEST_URI} !/files.* [NC]
RewriteCond %{REQUEST_URI} ![\.(jpg|png|jpeg|gif)].* [NC]
RewriteCond %{REQUEST_URI} !/userfiles.* [NC]
RewriteRule ^(.*)$ http://mobilesite.com/$1 [L,R=302]
Add other image extensions here if necessary:
RewriteCond %{REQUEST_URI} ![\.(jpg|png|jpeg|gif)].* [NC]

Redirect root of parked domain in one way, all other urls in another way, using htaccess

I recently got an alternate domain (parked) and would like it to behave like this:
on a request for "alt-domain.com/" (root): serve "main-domain.com/fakeroot" without a redirect (200)
on a request for "alt-domain.com/anyotherpage": serve "main-domain.com/anyotherpage" with a redirect (301)
I tried this, but it doesn't work:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
Each rule works on its own, but when both are present the request for root also get a redirect. I tried inverting the order, tried skipping [S=1], tried [NS], but no luck. That's when I realized I'm not an htaccess expert, nor a regex expert.
Any help would be much appreciated.
D.
The problem is that the L flag probably doesn't work like you expect, and when mod_rewrite re-examines your rules, RewriteCond %{REQUEST_URI} !^/$ matches because the %{REQUEST_URI} is updated to /fakeroot.php after the initial rewrite.
There are a few different ways to fix this, but I believe this should work well enough, and it doesn't involve changing much:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/(fakeroot\.php)?$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]

Resources