(.htaccess) allow one IP to httpdocs folder & redirect others to httpdocs\folder - apache

I would like to allow only my IP to my website (\httpdocs) and redirect all other requests to another folder (\httpdocs\folder). How and can this be achieved through .htaccess?
Currently I have redirect my domain to this folder, where the 'under construction ' page is, through plesk.

Try the following in your root .htaccess file. 123.123.123.123 is the IP address to allow. I assume /httpdocs is the document root on your filesystem and is not a public URL.
RewriteEngine On
RewriteCond %{REMOATE_ADDR} !=123.123.123.123
RewriteCond %{REQUEST_URI} !^/folder [NC]
RewriteRule .* /folder [R=302,L]
However, you should redirect to the actual "under construction" page, rather than just the "folder" that contains it.
Also, if this is "temporary maintenance" then consider implementing a 503 Service Unavailable instead:
https://webmasters.stackexchange.com/questions/55635/how-to-set-restrictions-on-my-website/55659#55659

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.

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

Point add-on domain to a subfolder and redirect direct access to that subfolder to add-on domain using .htaccess

I have a site example.com/main and another site example2.com.
I have set up an add-on domain for example2.com which shows files in the main subfolder in the example.com domain. What I want is that when users go to example2.com, they REMAIN on example2.com while they see files from example.com/main. So if they access other files like main/contact.php, it shows as example2.com/contact.php.
I have successfully done what I mentioned above in the add-on page of cPanel.
My issue:
I want it so that when users visit example.com/main directly, I want them to be redirected to example2.com and then see the main subfolder. I have had no luck as I keep getting stuck in a loop.
Doing Redirect 301 / http://example2.com/ doesn't work.
Thank you.
You can use:
RewriteCond %{HTTP_HOST} example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+main[\s/?] [NC]
RewriteRule ^main(/.*)?$ http://example2.com$1 [NC,R=301,L]

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.

rewrite rule base on first sub domain to directory

I need to redirect the first sub domain of any domain name that will hit the root folder of that web server to a subfolder.
It need to keep the incoming port (stupid isp that block port 80 and 443).
It need to be able to handle both http and https.
It need to ignore www.whateverthedomain.ext
Ex.
dom1 : mydummy.com
dom2 : toberedirected.net
dom3 : putanydomainhere.ca
possible incoming url
http(s)://firstsub.mydummy.com:8082
http(s)://whateveryoutypehere.thegoodsub.toberedirected.net
http(s)://firstsub.mydummy.com/firstsub/
http(s)://www.mydummy.com/
The result of the 3 incoming url should be
http(s)://firstsub.mydummy.com:8082/firstsub/
http(s)://thegoodsub.toberedirected.net/thegoodsub/ (i dont care what is in front of the first sub)
http(s)://firstsub.mydummy.com/firstsub/ (dont rewrite, it is not the root folder)
http(s)://www.mydummy.com/ (dont rewrite, ignore www and give default webpage of the domain)
It is probably easy to write but i cannot figure it out this morning.
Try adding this to the htaccess file in your document root
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.
RewriteRule ^/?$ /%1/ [L,R=301]
This should preserve the hostname and port when redirecting. It will only redirect requests for the web root, and will redirect to the name of the subdomain, with a trailing /.