How to allow access to only a single file? - apache

I have a apache machine which is serving a .js file. That file should be the only file that need to seen.
I have configured to do so in my apache like this :
<Location /var/www/test/test.js>
Order allow,deny
Allow from all
</Location>
The site address is test.in which points to test.js file in /var/www/test directory. That is working fine. But I wish when the user tries to hit test.in/someurl (which is not available) or some other url than test.in need to give an message with 401 error.
How do I do that? Thanks in advance.

You misused <Location> - the argument should be URI not the directory path... You should use <Directory> to get the expected behavior.
I would do something like this (you should finetune it, it shows just the principle):
# first deny access to everything
<Location />
Order Deny,Allow
Deny from All
</Location>
# then allow access to specific URL
<Location /test/test.js>
Order Allow,Deny
Allow from All
</Location>
Have a look on Order directive and one or more of following: Location, LocationMatch, Directory, DirectoryMatch, Files, FilesMatch, etc.

Related

Which folder to add a public accessible file in yii-framework?

I need to upload a public accessible file in yii installation which I can access from url. This is domain verification file. I've uploaded it in /frontend/web folder but it's not accessible directly via url mydomain.com/file.txt
As pointed out below, .htaccess file may be preventing this file to be accessible. Can anyone let me know how to write rule in .htaccess file to allow it?
This might help
<Files *.*>
Order Deny,Allow
Deny from all
</Files>
<Files ~ "^index\.php|css|js|.*\.png|.*\.jpg|.*\.gif|.*\.jpeg|.*\.ttf">
Allow from all
</Files>
If you get the following error.
You don't have permission to access /frontend/web/ on this server.
Then, add the following rule in your .htaccess
<Directory "/path/to/frontend/web/">
Options +Indexes
Allow from all
</Directory>
Here the documentation about Apache .htaccess permissions

Access Control Apache

Just having trouble setting an access control in Apache. So I want to deny access to a specific ip address for a section in my website. So when this ip address access my site, they shouldn't be able to see the "test" section of the website.
This is what I have done inside the httpd.conf file
<Directory /test>
Order Allow,Deny
Allow from all
Deny from 10.13.89.47
</Directory>
Please can someone tell me what I am doing wrong ?
Thanks
Based on the information you have provided, you document root is '/' (very dangerous) or you've not understood how the 'Directory' tag works. I would expect the tag to look something more like.....
<Directory /srv/www/htdocs/test>
I have used something else to get it working.
<Location/test>
Order Allow,Deny
Allow from all
Deny from 10.13.89.47
</Location>

Location and Directory cause 500 error

I have an .htacces file and I am trying to open up access to a file and folder within a protected folder.
The file is index.php so I do the following:
<Files index.php>
Order Allow,Deny
Allow from All
Satisfy Any
</Files>
This works and give me access to this file. This file requires assets from the assets/ directory. So I try to open that directory up by doing the following:
<Directory "/assets">
Order Allow,Deny
Allow from All
Satisfy Any
</Directory>
But this is giving me a 500 error. Not sure why.
You can't use the <Directory> container inside an htaccess file (which is essentially like the <Directory> container itself). If you want to allow access to assets, then create an htaccess file inassets with just:
Order Allow,Deny
Allow from All

Apache Location directive for dynamic content fails if nested path

I'm using Apache 2.2x. Most of the content is generated via mod_perl. So, it's dynamic content that has no filesystem mapping. Perfect use of < Location >.
Apache config:
<Location /finance_module1>
SetHandler perl-script
PerlResponseHandler Finance::Module1
</Location>
<Location /finance/module2>
SetHandler perl-script
PerlResponseHandler Finance::Module2
</Location>
Module1 works, and is shown here to show that my setup otherwise works.
Module2 does not work. Apache says "File does not exist: /home/joe/www/htdocs/finance". The only difference between the module configurations is that Module2 location contains multiple slashes (what I'm calling a nested path).
About the "File does not exist" error: Of course it doesn't exist -- it's a Location, not a File or Directory. So why does this happen?
I would like to be able to use paths with multiple slashes because I've got a lot of mod_perl modules, and it would be nice to categorize for purposes of control. For one trivial instance, robots.txt could simply say:
Disallow: /finance/
The Apache docs specifically state that < Location > directives need not map to the filesystem, and are well-suited for dynamically generated content.
What am I doing wrong? Is there a workaround? (Besides the obvious "just don't do that").
Thanks.
Answering my own question, for the benefit of anyone else wondering the same thing.
Short answer, use LocationMatch.
In the example above, say the urls are /finance/module1 and /finance/module2. Having the "finance/" path allows all the finance handlers to be configured as a group, in situations where that's desirable.
For example:
<LocationMatch /finance/.*>
SetHandler perl-script
PerlAccessHandler foo
</LocationMatch>
<Location /finance/module1>
SetHandler perl-script
PerlResponseHandler Finance::Module1
</Location>
<Location /finance/module2>
SetHandler perl-script
PerlResponseHandler Finance::Module2
</Location>
Slight typo perhaps?
<Location /finance_module1>
vs.
<Location /finance/module2>
Not sure if that is the issue.
Perhaps this (add to httpd.conf)
Alias /finance "path-to-files"
<Directory "path-to-files">
Options +Indexes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Then try the script. You could also make an empty folder there perhaps?

using apache location directive to list folders from trac

I have the following directory structure:
--var
----trac
------company1
--------project1
--------project2
------company2
--------project3
--------project4
and i was wondering if theres a way to specify in httpd.conf to list the directories when i go to domain.com/trac. Currently i wrote:
<Location /trac>
Options Indexes
</Location>
But i dont know how to specify the document root to /var/trac.
I tried to do
PythonOption TracEnvParentDir "/var/trac"
PythonOption TracUriRoot "/trac
but i get error 500, and i believe that is because the folders in /var/trac are not trac environments.
thanks.
I think you're right. You need to find a way to let Apache handle requests to "/" without the help of Python and trac.
It's a bit hard to give you advice because I don't know what your httpd.conf looks right now, but my trac-setup used a <LocationMatch> directive to catch everything that should not be handled by trac so Apache can take care of it.
So you could do something like this:
<LocationMatch "^/trac/.+">
# Your trac directives here
PythonHandler trac.web.modpython_frontend
....
</Location>
Alias /trac "/var/trac"
<Directory "/var/trac">
Options Indexes
Order allow,deny
Allow from all
</Directory>