htaccess redirect https traffic to a specific folder? - apache

I am hosting my website on godaddy and recently purchased an SSL cert so I can also run https traffic. However, I want all https traffic to go to a specific folder that regular http cannot get to. In addition, I don't want the specific folder to be a sub-folder of the main http document root.
GoDaddy cPanel folder structure:
/home/myAccountFolder/
/cache
/logs
/public_html
/private_html
...
By default, all standard http traffic for "www".foo.com will go to /public_html - but how can I get all traffic for https://api.foo.com to map to the /private_html folder?
I am told that .htaccess can do this but can't seem to locate how to do this. I know I could redirect https to /home/myAccountFolder/public_html/secure, but I want it going to /home/myAccountFolder/private_html - a completely different root folder.
Also, I don't have access to the server configuration or (that I am aware of) have the ability to create/manage virtual hosts within GoDaddy's cPanel enviornment. All I can do is control directories and what goes into my websites .htaccess file.

Inside your site root .htaccess you can use this rule to redirect api.foo.com to secure/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^api\. [NC]
RewriteRule !^api/ api%{REQUEST_URI} [L,NC]
Inside your api/.htaccess you can use this rule to allow only http://api.foo.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^api\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ - [F]
# rest of the rules

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]

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.

Redirect for path but only for specific domain

We have different domains that are hosted on our server. Recently one of the main sections of our site has been moved to another server and been given a subdomain:
http://www.mysite.com/store
Has been moved to
http://store.mysite.com
Within our apache VirtualHost we wanted to redirect all traffic from the old domain to the new one:
Redirect permanent /store http://store.mysite.com
The problem is, we have other hosted sites that are being redirected now:
http://www.othersite.com/store
http://api.greatsite.com/store
We don't want this. How can I only have apache do redirects if http://www.mysite.com/store which has the /store path, and ignore other domains with /store?
Use mod_rewrite based code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^/?store(/.*|)$ http://store.mysite.com [L,R=301,NC]

htaccess : how to redirect all files in a folder to non subdomain if accessed via a subdomain?

using htaccess, I need to remove the subdomain part of the url if a file is accessed in /folder/myfiles/
eg. I need to redirect
sub.domain.com/folder/myfiles/anyfile.php
to
domain.com/folder/myfiles/anyfile.php
any file found in /folder/myfiles/ if accessed via a subdomain must be redirected to the same file on the root domain.
background: the site uses subdomains for wordpress blogs on multisite. There is a 3rd party app installed to a folder on the root domain, the licence is only valid for the root domain and attempting to access it via a subdomain invalidates the licence so I need to make sure that any file in that folder must only be accessed via the root domain and not a subdomain.
In the folder's .htaccess:
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteRule (.*) http://domain.com/folder/myfiles/$1
In the root .htaccess or server config:
RewriteCond %{HTTP_HOST} sub.domain.com
RewriteRule ^/?(folder/myfiles(/.*)?)$ http://domain.com/$1