HTACCESS issue- domain vs subdomain - .htaccess

I have got lots of ideas from google and stackoverflow- but none of those was exactly what i am looking for. here is scenario-
I have bought a hosting space from a provider. I had to provide a domain name(let abc.com) as the primary domain of that hosting space.
Then i have found that i have to put all the contents for that rimary domain(abc.com) into the document root directly. that is no directory like www/abc or www/abc.com.
Then I googled and found lots of .htaccess solution. I picked the following one-
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} abc.com
RewriteCond %{REQUEST_URI} !^/abc.com/(.*) [NC]
RewriteRule ^(.*)$ /abc.com/$1
I have just paste above lines at the end of the existing .htaccess file (default).
It was working fine. I have been using www/abc.com directory for my abc.com domain from then.
Recently I have added some subdomains (let xyz.abc.com) to my abc.com domain. But it is behaving strange with me. all subdomains are looking for its contents from abc.com/subdomain (eg. abc.com/xyz.abc.com)
This time i am getting no solution over google (i may missed it).
Someone help me please- i am in bad shape.
EDITED:
Following lines were in WebRoot .htaccess from the beginning. After that I have added additional lines as mentioned above(3,4)
# -FrontPage-
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName abc.com
AuthUserFile /home/abc/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/abc/public_html/_vti_pvt/service.grp\
EDITED AGAIN:
There are some other domains(except the main domain abc.com) in the same hosting space. Those domains have some working sub-domains. But sub-domain of main domain is not working as i explained above.

Change
RewriteCond %{HTTP_HOST} abc.com
to
RewriteCond %{HTTP_HOST} ^abc.com$
Now the rules do not match your subdomains anymore.
Update
# catch www.abc.com and abc.com (and wwwwwwwwwww.abc.com)
RewriteCond %{HTTP_HOST} ^(w+\.)?abc\.com$

Related

how to restrict access to a file in .htaccess to a particular domain

I want to restrict access to a file "test.txt" with .htaccess
The problem, I'm on a multisite and I want only one domain to access to the file eg. mydomain.com/test.txt
When other domain trying to access, it should denied them: otherdomain.com/test.txt
Multisite is sharing the same files but different db.
<Files "test.txt">
Order deny,allow
deny from all
allow from mydomain.com
</Files>
You may use this deny rewrite rule instead of allow/deny:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(?:www\.)?mydomain\.com$ [NC]
RewriteRule ^test\.txt$ - [NC,F]

IP restriction for a certain domain if multiple domains point to same webroot (.htaccess)

Normally I create IP restrictions with adding following snippet in .htaccess:
<Limit GET POST>
order deny,allow
deny from all
allow from 23.98.431.9
allow from 123.456.78.9
allow from 9.876.54.32
allow from 555.333.2.33
</Limit>
Above snippet works perfectly if only one domain is pointing to webroot. But how can I add an IP restriction for only one domain if multiple domains point to the same webroot? For example:
www.example.com
wwww.examplewebsite.com
wwww.exampleawesome.com
All above domains are pointing to the same webroot. But now I need to restrict access for only www.example.com (certain IPs are allowed to see the site). How can I achieve this?
Thank you so much!
You can use mod_rewrute rules for this in root .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} ^(GET|POST)$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteCond %{REMOTE_ADDR} !^(23\.98\.431\.9|123\.456\.78\.9|9\.876\.54\.32|555\.333\.2\.33)$
RewriteRule ^ - [F]

htaccess to redirect from subdirectory to alternate domain

I'm trying to do a simple task with htaccess but I don't have any experiences with htaccess. Basically, this is what I'm trying to achieve.
I have a website (www.example1.com) with a hosting account. And now, I'm launching a second website (www.example2.com) that uses the same hosting account.
Physically the files for www.example2.com exist in www.example1.com/example2.
As you might expect, what I'm trying to achieve is that when a user writes www.example1.com/example2, they are redirected to www.example2.com.
This is what I've tried doing
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^/example1.com/
RewriteRule !^/example2.com/
And also...
Order Deny,Allow
Deny from all
Allow from example2.com
Any help would be appreciated!
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$ [NC]
RewriteRule ^example2(.*) http://www.example2.com$1 [R=301, L]
First line - check if the host matches example1.com with or without the "www.".
Second line - check if the path starts with example2 and, if it does, redirect to www.example2.com, maintaining any existing path after example2.

Use .htaccess to block all domains to access images folder except myown domain

i want to use .htacess to block all domains to access my images folder.
this folder is only accessible by own domain only.
I think what you are looking for is some sort of hotlink protection in which case this in your .htaccess file should work:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
Change yourdomain.com to whatever your domain is. Change the RewriteRule to include any file extensions you wish to block and then place the entire thing in a .htaccess file in the directory you wish to protect.
As described in this page of Apache docs, you can accomplish it with the following directives, replacing example\.com with your domain name (please remember to escape any dot characters from . to \.:
SetEnvIf Referer example\.com localreferer
<FilesMatch \.(jpe?g|png|gif)$>
Order deny,allow
Deny from all
Allow from env=localreferer
</FilesMatch>

.htaccess redirect help

I'm a complete newbie to .htaccess. I've been looking around trying to figure this out.
I'm trying to figure how to do the following:
1 web server hosting account
2 different website, call them abc.com and def.com
I want www.abc.com to continue to point to the webroot/Default.aspx. But I want www.def.com to point to www.abc.com/def/Default.aspx while www.def.com still shows up in the address bar.
The admin guy for the server farm I use stated I need to use an .htaccess file. I've been googling around trying to figure this out but only examples I find will redirect every website hit to a server to the subdirectory. I only want www.def.com calls to redirect to the subdirectory.
<Files .htaccess>
order allow,deny
deny from all
</Files>
RewriteCond %{REQUEST_URI} !^/def.com
RewriteCond %{HTTP_HOST} ^(www\.)?def\.
RewriteRule ^(.*)$ foldername/$1 [L]

Resources