Apache - .htaccess

Htaccess (HyperText Access) is a configuration file that allows developers to overide the Apache webserver configuration at directory level.

Enable .htaccess

By default .htaccess is not enable, we have to change in httpd.conf (Apache) configuration file to for enable .htaccess

Replace AllowOverride None to AllowOverride All

Example:

1<Directory /var/www/html/example.com/public_html>
2    Options Indexes FollowSymLinks
3    AllowOverride All
4    Require all granted
5</Directory>

Common Uses of .htaccess

  • Mod_Rewrite
  • Authentication
  • Custom Error Pages
  • Mime Types
  • SSI (Server Side Includes)

Mod_Rewrite

Mod_rewrite module is use for rule-based rewrite engine, based on Perl Compatible Regular Expressions. All the URL rewrite commands follow the below pattern

RewriteRule Pattern Substitution [OptionalFlags]

Example:

1RewriteRule ^products/([A-Za-z0-9-]+)/?$ results.php?products=$1 [NC]

Some useful Snippets

Disable or Enable Directory browsing

1# disable directory browsing
2Options All -Indexes
3# enable directory browsing
4Options All +Indexes

Change index file

1DirectoryIndex index.html index.cgi index.php

Remove www in Your URL

1RewriteEngine On
2RewriteBase /
3RewriteCond %{HTTP_HOST} ^www.anandvyas.in [NC]
4RewriteRule ^(.*)$ http://anandvyas.in/$1 [L,R=301]

Redirect Browser to https

1RewriteEngine On
2RewriteCond %{HTTPS} !on
3RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Force “File Save As” means to have force user to download file, not see in the browser

1AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4

Redirect mobile and tablets on different folders

1RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
2RewriteRule ^(.*)$ http://anandvyas.in/folderfortablets [R=301]
3RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$
4RewriteRule ^(.*)$ http://anandvyas.in/folderfortablets [R=301]

301 Permanent Redirects

1Redirect 301 /olddirectory/file.html http://www.anandvyas.in/newdirectory/file.html

Browser Cache

 1# 1 WEEK 1 DAY
 2<filesMatch “.(jpg|jpeg|png|gif|swf|ico)$”>
 3    Header set Cache-Control “max-age=691200, public”
 4</filesMatch>
 5
 6<filesMatch “.(xml|txt|js)$”>
 7    Header set Cache-Control “max-age=691200, proxy-revalidate”
 8</filesMatch>
 9
10<filesMatch “.(html|htm|css|php)$”>
11    Header set Cache-Control “max-age=691200, private, proxy-revalidate”
12</filesMatch>

Gzip Compression

 1# compress text, HTML, JavaScript, CSS, and XML
 2AddOutputFilterByType DEFLATE text/plain
 3AddOutputFilterByType DEFLATE text/html
 4AddOutputFilterByType DEFLATE text/xml
 5AddOutputFilterByType DEFLATE text/css
 6AddOutputFilterByType DEFLATE application/xml
 7AddOutputFilterByType DEFLATE application/xhtml+xml
 8AddOutputFilterByType DEFLATE application/rss+xml
 9AddOutputFilterByType DEFLATE application/javascript
10AddOutputFilterByType DEFLATE application/x-javascript

Browser specific Compression

1BrowserMatch ^Mozilla/4 gzip-only-text/html
2BrowserMatch ^Mozilla/4\.0[678] no-gzip
3BrowserMatch \bMSIE !no-gzip !gzip-only-text/html