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

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

Related

htaccess redirect https traffic to a specific folder?

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

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

301 redirect with domain both pointing to same directory

I am currently moving from one domain to another. Both domains are add on domains in CPanel and point to the same sub-directory.
How can i set up 301 redirect that redirects all of the old domains pages to the same file in the new domain. For example olddomain.com/example to newdomain.com/example. Since both domains reside in the same sub directory the both utilize the same .htaccess file.
Since both domains share the same .Htaccess file I'm having trouble getting the 301 redirect to work.
Put the following in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^newdomain.com
RewriteRule /(.*)$ http://newdomain.com/$1 [R,L]
It will check what your host-header is, and will redirect anything to newdomain.com if that's not the current site.

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.

On shared hosting subdomain not point to my website's folder on server

I have my website host on shared hosting server where other websites are also hosted.
Now I want to subdomain to point my website's folder on server.
Means if we go to the URL coolname.domain.com then it should be redirect to www.domain.com.
But It's redirected to Cpanel error page.
Thanks in advance.
The best way to do that is to set an entry in your DNS zone for your subdomain.
You can also use URL Rewriting in a .htaccess file :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule (.*) http://domain2.com/$1