Htaccess: Htaccess tips

A .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration.

Use of htaccess
Authorization, authentication
Rewriting URLs
Blocking
SSI
Directory listing
Customized error responses
MIME types
Cache-Control

Configuration apache for htaccess
There are two things to change in apache configuration (httpd.conf) which are ‘mod_rewrite’ and ‘AllowOverride’ for htaccess.
First, find httpd.conf file in web server.

In Window:
wampbinapacheapache2.2.6confhttpd.conf

open httpd.conf file in your editor.
First, enable mod_rewrite.
search mod_rewrite, you will find ‘LoadModule rewrite_module modules/mod_rewrite.so’ then remove # from it.

Second, enable AllowOverride
Search ‘AllowOverride’, you will find similar to below:

Options FollowSymLinks
AllowOverride none
Order deny,allow
Deny from all
Satisfy all

Now change AllowOverride from none to all.

Options FollowSymLinks
AllowOverride none
Order deny,allow
Deny from all
Satisfy all

Finally, restart apache.

In Linux:
Similarly, you can configure in linux in this file.
/etc/httpd/conf/httpd.conf

In Ubuntu:
Open a terminal and type the following command
$ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/
or
$sudo a2enmod rewrite

This will enable rewrite module for apache.

Now change AllowOverride from none to all.
$sudo gedit 000-default
find:

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

Change AllowOverride from None to All

Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

You need to restart apache afterward:
$sudo /etc/init.d/apache2 restart

Example:
Rewrite *.html to *.php(eg index.html to index.php,home.html to home.php )
Create a test directory and a PHP file that is index.php in test directory then write below code in this file

<?php echo "Test htaccess"; ?>

Create a .htaccess file in the same directory and write below code in this file
RewriteEngine on
RewriteRule ^(.*).html$ $1.php

Now you can run as similar to http://localhost/test/index.html

# To remove last trailing slash from the URL:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R]

# To redirect from one path to another:
Redirect /campaign/from/ https://www.tilkor.com/campaign/to/

# redirect contact-us/ to manage.php?action=contact-us
RewriteRule ^(.*)/$ manage.php?action=$1

# redirect contact-us to manage.php?action=contact-us
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ manage.php?action=$1 [L]

# Rewrite with two variables
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ manage.php?action=$1&variable=$2 [L]

# Enable leverage browser caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault “access plus 1 seconds”
ExpiresByType image/x-icon “access plus 2692000 seconds”
ExpiresByType image/jpeg “access plus 2692000 seconds”
ExpiresByType image/png “access plus 2692000 seconds”
ExpiresByType image/gif “access plus 2692000 seconds”
ExpiresByType application/x-shockwave-flash “access plus 2692000 seconds”
ExpiresByType text/css “access plus 2692000 seconds”
ExpiresByType text/javascript “access plus 2692000 seconds”
ExpiresByType application/x-javascript “access plus 2692000 seconds”
ExpiresByType text/html “access plus 600 seconds”
ExpiresByType application/xhtml+xml “access plus 600 seconds”
</IfModule>

Services should enable in the server using the mentioned command
sudo a2enmod expires
sudo a2enmod headers

Enjoy!!!!!!!!