Apache and Overriding With htaccess


.htaccess Overriding

With .htaccess, you can override the main configuration you set with Apache. However, you can be very specific with your needs. For example, you can set a domain to have no override options in the root folder, yet, specify which folders you want to have the ability to override the main settings.

The code block below will help simplify this explanation.To make this as simple as possible, there is the default file from an Apache installation on an Ubuntu installation on an Ubuntu server. The file is located in the path /etc/apache2/sites/available/default.

As you can see, there is no option to override anything in the root folder. But, the blog folder will be given the option to override. If you have mod_rewrite enabled, you would see that it will work for all files loaded from the blog folder since it has 'AllowOverride All'.

So, there you are. Only the files the blog folder will have the abilty to use the .htaccess file.

<Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        <Directory /var/www/blog>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

About mod_rewrite

When you rent a server; shared, VPS or dedicated, it lis likely that mod_rewrite is enabled by default. But, when you build your own you will likely need to enable it. At the same time, you may want to enable mod headers too.

sudo a2enmod rewrite
sudo a2enmod headers


Using .htaccess code with Apache Stanza

For the real security type, you can always keep all rules in a safe place on the server and deny any overriding with .htaccess. The code below shows code within the file called /etc/apache2/sites/available/default.

<Directory /var/www/blog-test>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all

DirectoryIndex static.php index.php index.html

#Expire Header
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
#ExpiresDefault "access plus 2 hours"

#ExpiresActive On
#ExpiresByType image/gif "access plus 1 month"
#ExpiresByType image/png "access plus 1 month"
#ExpiresByType image/jpeg "access plus 1 month"
#ExpiresByType text/css "access plus 1 month"
#ExpiresByType text/javascript "access plus 1 month"
#ExpiresByType application/x-javascript "access plus 1 month"
#ExpiresByType application/x-shockwave-flash "access plus 1 month"

</FilesMatch>

<IfModule mod_rewrite.c> 
Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ blog/index\.php?title=$1 [NC]


#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)\.html$ index\.php?title=$1 [NC]
</IfModule> 

<Files ~ ".inc"> 
Order allow,deny 
Deny from all 
</Files>

<Files ~ \"\\.(inc|sql|...other_extensions...)$\">
  order allow,deny
  deny from all
</Files>

<Files 403.shtml>
order allow,deny
allow from all
</Files>
        </Directory>