Disable directory listings in all but one folder, using htaccess? - .htaccess

Right now, I'm using Options -Indexes to hide access to all directory listings on a website of mine.
What I've discovered however is that I need directory access to a particular folder:
../Resource/Other/
Is there any way of applying a logical NOT rule to htaccess to allow access to certain folders, while disabling directory access by default? Or do I have to approach it from the other angle and enable directory listings globally and then selectively disable them folder by folder?

Create a htaccess file with Options +Indexes in the folder you want to list.
Be sure to remove any index files too.

the almost easiest way to do it is disabling listings globally and then allow list others. That is, your virtual host should be set up by default not to list directories when removing the "Indexes" option. Then you would add a Directory directive and set "Options" to allow listing of a particular directory.
For instance:
Say you have the following directory structure:
/home/user/www (note: www is the document root).
Into www directory there are appdir1, appdir2, app3 directories and you want list only appdir3 so in your virtualost:
<VirtualHost *:80>
DocumentRoot /home/user/www
ServerName myserver.local
<Directory /home/user/www/>
Options FollowSymLinks MultiViews
</Directory>
</VirtualHost>
In this case is not possible directory listing, any directories under document root is forbidden.
However, if you add other Directory directive you can set directory listing to specif dir:
<VirtualHost *:80>
DocumentRoot /home/user/www
ServerName myserver.local
<Directory /home/user/www/>
Options FollowSymLinks MultiViews
</Directory>
<Directory /home/user/www/appdir3>
Options Indexes
</Directory>
</VirtualHost>
On the other hand you can add .htaccess files to directories that you want allow directory listing.
In /home/user/www/appdir3/.htaccess add:
Options +Indexes
Also, if you runs Apache version 2.4+ you should take a look the <If> directive.
<If> Directive

Related

How to redirect wildcard subdomain to my domain

I would like to redirect wildcard subdomain to my actual domain.
like this (I already have error.example.com)
dsfas.example.com -> error.example.com
but I could not do this after hours of searching. Is there a solution to this.
I am using apache
You can do this by creating a “catch-all” Apache configuration and redirecting to the domain you specify.
Create an Apache configuration file with a name like zzz-catchall.conf (so that it’s the last file alphabetically in the directory) and paste this into the file:
<VirtualHost *:80>
ServerAdmin none#noaddy.net
DocumentRoot /var/www/catch/public
ServerName catch.example.com
ServerAlias *.example.com *.*
DirectoryIndex index.php index.html
ErrorLog ${APACHE_LOG_DIR}/catch-error.log
CustomLog ${APACHE_LOG_DIR}/catch-access.log combined
<Directory /var/www/catch/public>
Options FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow From All
</Directory>
</VirtualHost>
Be sure to change the domain names and directories accordingly.
In the /var/www/catch/public (or whatever) directory, you can set up a redirect a number of ways, but .htaccess would be simple enough:
Redirect 301 / https://error.example.com/
Ensure the virtual host configuration file is enabled, and reload Apache. That should get you going 👍🏻

mod_rewrite is enabled, .htaccess file is ok, but requests are not going through index.php

I've just installed ubuntu-server_6.04.2_LTS, after the fresh installation I've enabled mod_rewrite, it's showing in phpinfo()
my .htaccess file is OK
but, still requests are not going through index.php
You should check if for that specific directory (root folder of your project), apache is configured to allow overriding through .htaccess.
If you want to allow such override, the directives found in apache.conf or the specific settings for your virtual host should look like this:
<Directory /var/www/>
AllowOverride All
Require all granted
</Directory>

.htcaccess in Apache not working

I enabled mod_rewrite and want to redirect one of my local virtual host websites to google.com for practice.
I created an .htcaccess file in my virtual directory (/var/www/example.com/.htcaccess) and gave permissions to .htcaccess by adding the following code to my virtual host's (example.com) config in /sites-available/.
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
I am now attempting to use mod_rewrite by adding code to the .htcaccess file but have had no success thus far. Is there any way to check if .htcaccess is working? If it is, how do I redirect using mod_rewrite?
There is an typo. Normally the file name is .htaccess, not .htcaccess.
An other option is to set the file-name to you "personal style", like:
AccessFileName .htcaccess
http://httpd.apache.org/docs/2.2/howto/htaccess.html

.htaccess not being read

Iam trying to redirect my home page or any other page on the site to a particular php page .
This is my htaccess
Redirect 301 http://test.com/info http://test.com/get_forms_data.php
Options +FollowSymlinks
RewriteEngine ON
RewriteRule ^test.php$ http://test.com/get_forms_data.php [R=301,L]
I have checked my apache server .rewrite is enabled .
It still doesnt work .
If no matter what you put into your .htaccess file, you don't even get an error, that means that you probably need to have
AllowOverride All
set in your site configuration.
If you're on ubuntu, the place to look for the configuration is /etc/apache2/sites-available/. There you should find a file called default if this is a stock install of the default LAMP stack (https://help.ubuntu.com/community/ApacheMySQLPHP).
The key part there is this:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Now change AllowOverride None to AllowOverride All. After that don't forget to restart your apache like so:
$ service apache2 restart
As an addition to Morgan's answer, putting AllowOverride All in your virtual host is sometimes not enough. I had this in my virtual host:
<VirtualHost *:80>
...
<Directory />
...
AllowOverride All
...
</Directory>
</VirtualHost>
You would expect this to work, wouldn't you, <Directory /> means it should be applied to everywhere on the file system. But .htaccess was still being ignored. Restarting the server did not help. I put junk in the .htaccess file to confirm it was not being read.
My mistake was assuming a virtual host overrides the global configuration. Kind of it does: my above configuration overrides any global settings for the / directory. But the global configuration overrides it back for /var/www/ and below. My fix is:
<VirtualHost *:80>
...
<Directory /var/www>
...
AllowOverride All
...
</Directory>
</VirtualHost>
(this assumes none of the other configuration needed to apply outside /var/www; if it does, make a separate <Directory /> block for just that special configuration.)
I was struggling with the same problem, and Darren Cook's answer gave me the definitive clue to find the solution.
My app was in some folder out of th public www path, lt's say in /opt/my_app.
I couldn't create a VirtualHost, so I created a symlink in Apache's public www ponting to my folder:
/var/www/html/my_app -> /opt/my_app
The thing is, in my App's Apache config file, I was specifying:
<Directory /opt/my_app>
AllowOverride All
</Directory>
And my .htaccess file wasn't being read. Then I saw that in Apache's configuration there was this:
<Directory /var/www/html>
AllowOverride None
</Directory>
Then I realised that Apache config files do not care about symlinks, and therefore the general rule was being applied to my App's foler. I changed Directory to:
<Directory /var/www/html/my_app>
AllowOverride All
</Directory>
And everything worked.
If Redirection doesn't work inspite of updating apache2.conf
According to the accepted answer, I updated AllowOverride None to AllowOverride All in the apache2.conf file, however redirection via .htaccess file was still not working for me!
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All # did not work inspite of setting to "All"
Order allow,deny
allow from all
</Directory>
What worked for me...
I had to also enable module redirection
// enable module redirection
sudo a2enmod rewrite
Of course, do not to forget to restart your apache server for the changes to take effect
Reference
Assuming /var/www/html is the working directory:
Change from AllowOverride None to AllowOverride All
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ubuntu returns 500 error when .htaccess enabled

I'm currently moving a dev magento build to my live test subdomain on ubuntu and using Easy Hosting Control Panel (EHCP).
If I rename my .htaccess file to .htaccess and refresh my subdomain url, I get an internal server error 500. If I rename the .htaccess file to .htaccess.bak, and refresh, the subdomain displays my magento store fine. BUT, if I try to navigate, since the index.php isn't removed in the .htaccess file, all my links are dead unless I insert index.php between my root url and the actual page/directory I'm navigating to.
I tried over riding the /etc/apache2/sites-available/000-default and /etc/apache2/sites-available/default files to AllowOverride All:
<VirtualHost *>
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/vhosts/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
<FilesMatch "access_log|error_log">
Deny from All
</FilesMatch>
ErrorLog /var/log/apache2/error.log
LogLevel debug
CustomLog /var/log/apache2/access.log combineddefault
but changing that in either file, or both, and then reenabling the .htaccess file still gives me the 500 error.
anything I'm doing wrong here? Somewhere else I should be placing the mod rewrite information?
thanks!
I know this is kind of dead but for those with this issue check your /etc/apache2/mods-enabled to see if rewrite.load is there if not try to copy it from the /etc/apache2/mods-available folder that fixed the issue for me. You will also have to have the .htaccess override enabled for that
is it rules available?
maybe apache is disabled rewrite mod,you can try to enable.
type 'a2enmod rewrite' to turn on.

Resources