Skip to main content

.htaccess file

What is .htaccess file?
A .htaccess file is an ASCII file. It is a short form of 
Hypertext Access.
It is a configuration file used by Apache-based web servers. The .htaccess is the file’s extension. It is not a file name. It will affect on the folder in which it is located and also on subfolders.

we can use .htaccess for following

  • Remove file extension
  • Redirect to custom error pages
  • Block or allow IP addresses
  • Prevent directory listings
How to remove .php extension from URL?
Create .htaccess file in root of your project and add following code in it
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php


Before  URL is like www.example.com/home.php
After adding .htaccess it will be like www.example.com/home

Redirect to custom error pages
It is used to redirect the user to a custom error page by using the error code.
Following are error codes
  • 400 – Bad request
  • 401 – Authorization Required
  • 403 – Forbidden
  • 404 – File Not Found
  • 500 – Internal Server Error
ErrorDocument 404 /path/404.php
ErrorDocument 505 /path/505.php

Block or allow IP address
order allow,deny
deny from 192.168.1.1
allow from 192.168.1.2

Prevent directory listings
.htaccess also used to prevent folders to access directly by an end user.
Options All -Indexes

Comments

Popular posts from this blog

Web Development

                         Web development means creating websites. Websites can be accessible on Internet. Website is collection of web pages. It may contain any information, images, videos, forms. Web pages can be created by using a programming languages.  There are 3 main parts of web development 1. Client side scripting - It includes code which runs at browser side. It is user interface. What will user see and how will he interact with system is designed here. Languages includes in client side scripting : HTML CSS Javascript JQuery 2. Server side scripting - It includes code which runs at server side. User sends a request to server, server will process on it and send response to browser. Server side scripting allows the database on the web server to communicate with the web browser of the end user’s computer. Languages includes in server side scripting : PHP JAVA Python ASP.NET Ruby 3. Database - A database is used to store user's data, code, images,

Database handling in PHP

There are three method for handling database in PHP 1. MySQLi-  object-oriented 2. MySQLi -  procedural -oriented 3. PDO - PHP Database Objec t First create variable required $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "sample_db"; Method 1 : -  MySQLi -  object-oriented Create connection : - $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) {     die("Connection failed: " . $conn->connect_error); } Fetch the records from table  : - $sql = "SELECT * FROM users"; $result = $conn->query($sql); get rows count  : - $count = $result->num_rows; iterate through records  : - while($row = $result->fetch_assoc()) { //print single row details print_r($row); } close connection  : - $conn->close(); Method 2 :-  MySQLi -  procedural -oriented Create connection $conn = mysqli_con

Send SMS using cURL in PHP

function sendSMS($mobile,$msg) {      // configuration according to SMS gateway        $postData= array(             'user' => 'usrename',             'password' => 'password',             'mobiles' => $mobile,             'sender_id' => 'sender id',             'message' => $msg         );     // initialization       $ch = curl_init();       curl_setopt_array($ch, array(             CURLOPT_URL => 'url of sms gateway',             CURLOPT_RETURNTRANSFER => true,             CURLOPT_POST => true,             CURLOPT_POSTFIELDS => $postData         ));         //Ignore SSL certificate verification         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);         //get response         $output = curl_exec($ch);         //Print  the error if any         if (curl_errno($ch)) {             echo 'error:' . cur