Tuesday, July 23, 2013

Sample .htaccess file

RewriteEngine On

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php


## forward to www.portal.com if need
RewriteRule ^(.*)$ http://www.portal.com/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]

# if url start with 'index' then execute index.php with params
RewriteRule ^index([a-zA-Z0-9_-\s]+)$ index.php

## if you hit in browser a.html, execute a.php
RewriteRule ^(.*).html$ $1.php

## if you hit cinema execute index.php, or hit pritom execute pritom.php
RewriteRule ^cinema$ index.php
RewriteRule ^pritom$ pritom.php

## if you hit /menu/1/2/3/4, execute menu.php with parameters
RewriteRule ^menu\/(.*)\/(.*)\/(.*)\/(.*)$          menu.php?param1=$1&param2=$2&param3=$3&param4=$4
RewriteRule ^menu\/(.*)\/(.*)\/(.*)$                menu.php?param1=$1&param2=$2&param3=$3
RewriteRule ^menu\/(.*)\/(.*)$                      menu.php?param1=$1&param2=$2
RewriteRule ^menu\/(.*)$                            menu.php?param1=$1

## if not file or directory, execute error.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.+)$ error.php

## before execute any php file, execute before_action.php
php_value auto_prepend_file before_action.php

## after execute any php file, execute after_action.php if immediate file not end with exit
php_value auto_append_file after_action.php

## if not running with https forward to https
RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

## need user authentication
AuthUserFile C:\\xampp\\htdocs\\test\\.htpasswd
AuthName "Authorization Required"
AuthType Basic
require valid-user

1 comment:

  1. Nice article, thank you for sharing the informative article. I have also an article about htaccess
    . Thank you!!!

    ReplyDelete