On apache how do I allow access to only to a single file?

8,117

The Location directive matches URIs, not directly related to files on the filesystem. I suggest to use Directory and Files instead, like this:

<Directory "/var/www/test">
    Order deny,allow
    Deny from all
    <Files "test.js">
        Order allow,deny
        Allow from all
    </Files>
</Directory>
Share:
8,117

Related videos on Youtube

sriram
Author by

sriram

Updated on September 18, 2022

Comments

  • sriram
    sriram over 1 year

    I have a apache machine which is serving a .js file. That file should be the only file that need to seen.

    Edit:

    I have configured to do so in my apache like this :

    <VirtualHost *:7090>
            ServerAdmin [email protected]
            DirectoryIndex test.js
            DocumentRoot /var/www/test
            ServerName test.in
    </VirtualHost>
    
    
    <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?

    • Nils
      Nils over 11 years
      "points to" is what? A symlink in the filesystem, a redirect, a rewrite?
    • Nils
      Nils over 11 years
      I still dont get it. DocumentRoot` can point to a directory. Not to a file...
    • sriram
      sriram over 11 years
      @Nils: I have edited my question and added the code which I used to serve the file
  • sriram
    sriram over 11 years
    Nope now I cant even able to access the test.in. I get forbidden error!
  • gertvdijk
    gertvdijk over 11 years
    @sriram That was exactly your request if I read correctly. "That file should be the only file that need to seen." So all other files including the default index files should not be accessible, right?
  • sriram
    sriram over 11 years
    ya but test.in should be given access to the file test.js. But now I couldn't be able to do that.
  • ravi yarlagadda
    ravi yarlagadda over 11 years
    @gertvdijk <Files> can't have directory paths - try <Files "test.js"> within the <Directory> block?
  • sriram
    sriram over 11 years
    Still this doesn't worked@!