Block file from accessing using .htaccess for a subdomain - .htaccess

I have a website for e.g.
www.domain.com
And I have another subdomain for e.g.
test.domain.com
Both my main and subdomain point to same directory which is public_html.
I want to block access to
test.domain.com/test.jpg
but not
domain.com/test.jpg
Please note that this test.jpg is same file for both main and subdomain as they both point to same directory. So how do I block it for subdomain but not for main domain using .htaccess? Or is there any other way of doing that?
Thanks

An .htaccess file will be used regardless of the virtual host (unless explicitly deactivated using AllowOverride None), therefore you'll have to put:
<Files /full/path/to/test.jpg>
Deny From All
</Files>
in the apache configuration file inside the test.domain.com virtual host configuration.

Related

Sub-domain forwarding with masking via .htaccess

My sub-domain sub.domain.com is forwarding (redirecting) to another.website.com correctly. However, I want to enable masking, meaning that after redirecting I want my sub-domain to show in the address bar rather than the other domain, while still showing the other domain's page content.
What should I put in the .htaccess file in order to perform this action?
Also, note that the .htaccess file is under my domain's file manager in cpanel and not subdomain's (maybe that's a given for you, but bear with my basic knowledge please...).
Should you need any more information do not hesitate to point it out please.
Thanks in advance!!
You should check the mod_proxy module, the ProxyPass directive and ProxyPassReverse directive. Put these directives in config file:
ProxyPass / https://another.website.com
ProxyPassReverse / https://another.website.com

500 server error when denying access to subdirectory

I am trying to deny direct access to a subdirectory using .htaccess:
# Deny direct access
<Directory "/cron">
Deny from all
</Directory>
The above code is in the .htaccess file of my public_html directory. The subdirectory I am trying to block is located at public_html/cron.
When I enter an address within the /cron folder directly in the browser, I get a 500 server error instead of a 403. Any idea why?
You can't use <Directory> in .htaccess files.
http://httpd.apache.org/docs/2.2/en/mod/core.html#directory
Context: server config, virtual host
Use in your /cron/.htaccessfile:
deny from all

Intranet IP Lockdown

I only want people to access the intranet from my IP.
I have searched different blogs as well but not get more.
I just want my website to be accessed by just my IP address to make it secure. I need an intranet that can be locked down by IP.
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs), changing the 999... to your IP address:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^999.999.999.999
RewriteRule ^ - [F]
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

How Change Subdomain Root Folder to Public_html via .htaccess?

I have a subdomain that I want to make the root folder to "Public_html",
for example, my root subdomain is
- /public_html/subdomain
So I want to change that path to just /public_html/ for my subdomain. I know it can be done with .htaccess, but I dont know how to do it. Thanks...
I don't know exactly what you want, but here are my two answers:
Answer 1: You want your DocumentRoot to point to public_html
In this case add/change the DocumentRoot in your (virtual) host config of your webserver (I'm assuming you're using Apache, which would be /etc/httpd/conf/httpd.conf or /etc/httpd/conf/extra/httpd-vhosts.conf or even /etc/apache2/sites-enabled/yourhost)
Answer 2: You want to redirect from your subdomain folder to the root of public_html
This is not possible via a simple RewriteRule due to the fact, that your host is pointing to the subdomain folder. The other way round would possible (redirecting from public_html to subdomain).
What you can to is to create a symbolic link from subdomain which points to public_html or a file in it - but I don't recommend this.
Furthermore resources:
A brief description on how to forward to a subfolder using Rewrite rules
Another brief description about the DocumentRoot

How to disable effects of .htaccess within a subfolder if there is a .htaccess file in root?

In my application I have separate spaces for user and admin like
if www.example.com is my website, then www.example.com/admin is my admin URL.
I am using a .htaccess file in my root, and it affects some of the functionality in my admin folder, which I don't want to.
For example, consider below is my folder structure
..
.htaccess
index.php
admin
So if I don't want the .htaccess rules to apply within the admin folder, is there any way?
For people that don't have direct access to httpd.conf (shared hosting for example), just put another .htaccess file in the subfolder and set to the desired behavior.
You should be able to do this, but it does require write access to the httpd.conf configuration.
If you have access to the httpd.conf file, something like
<Directory /admin>
AllowOverride None
</Directory
should do the trick.
Also, note that using .htaccess files in the root directory (as you said you did) is not a recommended approach. You'd be better off moving the contents of the htaccess file into the proper contexts of the httpd.conf file.
More information can be found at http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride

Resources