DNS/.htaccess files to redirect subdomain to specific folder - apache

I'm trying to redirect my subdomain to a flder. I think I have the .htaccess code from another post here. Something like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^clients\.example\.com$
RewriteRule ^ http://example.com/clients/root/app%{REQUEST_URI} [L,P]
What DNS settings should I set up for the clients. subdomain to tie it in with the .htaccess.

You need to create a CNAME to point clients.example.com to the same server as example.com. Then you need to make sure your webserver is setup to serve the clients.example.com in the same root as example.com.
Of, you can simply setup clients.example.com's vhost to point to the /clients/root/app/ folder instead. There's a directive called DocumentRoot that you'd use to point a vhost to a where it's root folder is.
In the case of the first instance, where both the main domain and subdomain point to the same root, you don't need to use the P flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^clients\.example\.com$
RewriteRule ^ /clients/root/app%{REQUEST_URI} [L]
Is good enough and circumvents the need to go through mod_proxy.

Related

htaccess to redirect root to subfolder

We have a GoDaddy hosting. Within public_html we have a number of folders, each for a different site.
The main site (mysite.com) should normally be in the public_html but we moved them to public_html/mysite. We had an htaccess file to handle this change.
mysite.com is a slim (PHP) site. Within its public folder is another htaccess file that redirects to the public subfolder.
We tried to redirect mysite to a different web address and something got broken. Now if I go to mysite.com it shows Forbidden. Also, the FTP no longer works. When I try to connect with Filezilla I get
Connection attempt failed with "EAI_NONAME - Neither nodename nor servname provided, or not known".
This is because I try to connect to ftp.mysite.com. If I change that to an IP it does work. But clearly something is off with mysite.com.
The current htaccess within public_html/mysite is as follows:
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(?!public)(.*)$ /public/$1 [L]
Any ideas on how can I get this to work again? I think the problem is that the domain is pointing to the root of the public_html folder instead of public_html/mysite but no idea how to fix this.
UPDATE
We have an htaccess file in the public_html folder
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(?!mysite)(.*)$ /mysite/$1 [L]
If I remove it, all subdomains work but mysite.com shows Forbidden. If I enable the file, then every single subdomain, including mysite.com shows Internal Server Error.
You may have this .htaccess in site root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^(?!mysite/)(.*)$ mysite/$1 [L,NC]
You should remove RewriteOptions inherit directive as that is not serving any purpose here.
Also note some minor fixes in your rewrite rule.

How to add a rewrite rule for domain and it's subdomains in Plesk 11.0.9?

I need to redirect a request file from all possible subdomains to one file which is located in top level domain:
^.example.com/robots.txt >> http://example.com/robots.txt
The robots.txt may or may not exist in the subdomain httpdocs, this rule must be exucuted first (we want to block all crawlers in advance).
The folder structure provided by Plesk 11.0.9 is:
example.com/
...
httpdocs/
subdomains/
sub1/
httpdocs/
example.com is owned by root.
Where can I add an apache rewrite rule that would be trigger for all subdomains?
Edit: I tested with ping doesntexist.example.com and the request does get directed to example.com which means teoretically there should be a point in processing to execute the rewrite logic.
Try placing this rule in /example.com/httpdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)[^.]+\.(example\.com)$ [NC]
RewriteRule ^robots\.txt$ http://%1/$0 [L,R=302,NC]

Change document root folder in shared server with .htaccess

I'm the admin of an apache server (on a hosting package that allows me to host multiple domains), I've got one domain in public_html (let's call it www.ROOTwebsite.com) and 9 other domains hosted in a folder in the same directory level as the public_html, called DOMAINS.
So the structure is:
-DOMAINS/site1.com/
/site2.com/ ... etc
-public_html
I'm using '/' in the beginning of all relative paths in wamp for site1.com (for example /menu.php) and it works fine, but when I upload to DOMAINS/site1.com/ it messes up the site because it obviously is looking at the public_html directory as the ROOT.
I've used a number of combinations on the following in the .htaccess file, but I can't figure out which is the right syntax to change the ROOT to a directory sitting NEXT to public_html, not under it as usual:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.ROOTwebsite.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^ROOTwebsite.gr$
RewriteCond %{REQUEST_URI} !DOMAINS/site1.com/
RewriteRule (.*) /DOMAINS/site1.com/$1 [L]
I wish to change the root directory for site1.com so that it also works with the '/', without affecting the public_html website.
Can anyone see the problem with the code above?
You cannot redefine the document root via .htaccess. You have to create different vhosts for the domains that then can have separate document roots.
The closest I have come to redefine a document root via .htaccess is the following (not exactly what you are asking, because in my example, site1.com is a subdir of the main document root, but this would achieve what you want, with the caveat below).
RewriteCond %{HTTP_HOST} ^site1.com$ [NC]
RewriteRule !^site1.com/ /site1.com%{REQUEST_URI} [L,NC]
this will effectively redirect all site1.com to the site1.com subdirectory.
[caveat] The only problem I could not solve is how to redirect an url that goes directly to the subdirectory such as http://site1.com/site1.com/index.html to http://site1.com/index.html

Blocking access to a folder through http

I hope this question is not redundant, but I could not find any answer to the exact question I am asking.
I own a multiple site shared hosting plan on a Linux server.
The hosting plan refers to a domain that we can call www.domain1.com
Every other domain is hosted as a subdir of the main domain. so for example domain2.com will be hosted like: www.domain1.com/domain2.com/
It could be reached using that path or using www.domain2.com directly.
I want to disable the access to domain2.com as subfolder of domain1.com.
I suppose I need to put a .htaccess file in every subfolder of domain1.com which is hosting a website. Am I correct? What do I have to put in the .htaccess file?
thank you in advance,
Max
Put these rules in the htaccess file in domain1's document root (the parent folder):
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1\.com$ [NC]
RewriteRule ^(domain2\.com|domain3\.com)/ - [L,F]
Where the domain2\.com etc are the subfolders that you don't want to be able to have direct access.
However, if you have rewrite rules in each of those subfolders, then you do need to add rules in each of the subfolders:
RewriteCond %{HTTP_HOST} domain1\.com$ [NC]
RewriteRule - ^ [L,F]
The F flag will cause the request to return a 403 Forbidden.

Using .htaccess file to redirect to a subdomain

In my root folder, I have a home directory. I'd like all requests for mydomain.com or www.mydomain.com to be forwarded to home.mydomain.com. In my cPanel, I have already set up the sub-domain home.mydomain.com to point to mydomain.com/home.
Here's what I currently have in my .htaccess file:
RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteRule ^(.*)$ \/home\/$1 [L]
This successfully forwards mydomain.com and www.mydomain.com to mydomain.com/home or www.mydomain.com/home, respectively. But home.mydomain.com/filename gives me an internal server error, instead of serving the file at mydomain.com/home/filename.
I'm not sure I understand the exact requirements, but it seems you want to rewrite all (www.)mydomain.com requests to the /home directory while your subdomain home.mydomain.com already points to that directory and thus should be exempt from that rewrite directive. If for some reason (www.)mydomain.com can't be set up to point to /home as well, you'd need a Rewrite Condition, something like
RewriteEngine On
Options +SymLinksIfOwnerMatch
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ /home/$1 [L]
I also removed the backslashes which should not be needed. You can use the htaccess tester to check rewrite rules easily online.