Tricks & Tips for .htaccess

.htaccess is a configuration file for use on web servers running the Apache Web Server software. These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. .htaccess in commonly used by almost frameworks such as CodeIgniter, CakePHP etc. I am describing how certain things can be done through .htaccess file. In .htaccess file # is used to creating comments.

Note: .htaccess file will be hidden.

Disable directory Listing
If you are trying to open a directory which does not contain any index/default file then by default it will shows list of files exist in this directory. We can disable this listing of files.

# Disable Directory Listing
Options All -Indexes

Error Pages
If you want to create custom error message for errors like 404 (Page Not Found), 500 (International Server Error) then we need to use below snippet in your .htaccess file. I am assuming error.php is exist on root level

# Error Pages
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php

Domain Redirection
Below snippet is used when you want to add Domain redirection in your site. This redirection include redirection to WWW, sub-domain or even other sites.

Note: For this you must need to enable mod_rewrite extension in your php.ini file.

Redirect mydomain.com to www.mydomain.com.

# Redirect to WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]

Redirect old.mydomain.com to mydomain.com/old_data.

# Redirect to Sub-domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/old_data/
RewriteRule (.*) /old_data/$1

Redirect olddomain.com to newdomain.com.

# Redirect to New Domain
RewriteEngine on
Redirect 301 / http://www.example.com/

Use different file extension
Some time you may need to hide file extention or show different file extension. In this situation below snippet helps. In which we are using .html extention instead of .php extension. Means it will redirect mydomain.com/file.html to mydomain.com/file.php.

# Masking Extension
RewriteEngine on
RedirectMatch 301 (.*)\.html$ http://www.mydomain.com$1.php

Remove index.php
Some time you don’t want to show index.php when you are on root. Below snippet helps you in this time. It will redirect mydomain.com/index.php to mydomain.com/.

# Remove index.php from URL
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

Remove Trailing Slash
If you want to remove trailing slash from URL then below snippet helps you in this time. It will redirect www.mydomain.com/mydir/ to www.mydomain.com/mydir.

# Remove Trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.mydomain.com/$1 [R=301,L]

Add Trailing Slash
Sometime you may want to add trailing slash in URL, which is perfectly opposite of above, then below snippet helps you in this time. It will redirect www.mydomain.com/mydir to www.mydomain.com/mydir/.

# Add Trailing slash
RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

Leave a Reply

Your email address will not be published. Required fields are marked *