Monday, 14 March 2016

.htaccess RewriteRule Examples

.htaccess RewriteRule Examples

In Web Development, Many times we need to make Query string parameter as SEO Friendly, For this we need to make a changes in htacess file.
Benefits of SEO Friendly URLs.
  1. URL Readability become better
  2. Better Indexing by Search Engines
  3. Its hard to memorise the query string parameter
  4. Query string paramter in URL looks un-professional


Following are useful mod_rewrite RewriteRule EXAMPLES which you can use in your website.
Example 1
Original URL: http://domain.com/search.php?page=1
Rewritten URL:http://domain.com/search.php/page/1
.htaccess Rule:
RewriteEngine On
RewriteRule ^search.php/page/([0-9]+)?$ /search.php?page=$1 [L]



Example 2(Same as Above AND ".php")
Original URL:http://domain.com/search.php?page=1
Rewritten URL: http://domain.com/search/page/1
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/page/([0-9]+)?$ /search.php?page=$1 [L]



Example 3
Original URL:http://domain.com/search.php?category=vehicles
Rewritten URL:http://domain.com/search/category/vehicles
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/category/([a-zA-Z0-9]+)$ /search.php?category=$1 [L]



Example 4
Original URL: http://domain.com/search.php?category=vehical&page=1
Rewritten URL: http://domain.com/search/category/vehicles/page/1
.htaccess RULE:
RewriteEngine On
RewriteRule ^search/category/([a-zA-Z0-9]+)+/page/([0-9]+)?$ /search.php?category=$1&page=$2 [L]



Example 5
Original URL:http://domain.com/search.php?gender=men&department=clothing&products=tshirts[L]
Rewritten URL: http://domain.com/buy/men/clothing/tshirts
.htaccess RULE:
RewriteEngine On
RewriteRule ^buy/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$  /search.php?gender=$1&department=$2&products=$3[L]



Example 6
Original URL: http://domain.com/search.php?gender=men&department=clothing&products=tshirts&page=1
Rewritten URL:http://domain.com/buy/men/clothing/tshirts/page/2
.htaccess RULE:
RewriteEngine On
RewriteRule ^buy/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/page/([0-9]+)?$ /search.php?gender=$1&department=$2&products=$3&page=$4[L]


Note: You NEED NOT to include "RewriteEngine On" every time, Include once at top of .htacess file


Friday, 4 March 2016

AES Encryption and Decryption in PHP See example

AES Encryption and Decryption in PHP See example

Question: What is AES encryption?
Advanced Encryption Standard is a specification for the encryption of data established by the NIST (National Institute of Standards and Technology) in 2001.


Question: What are different key sizes availbles?
  1. 128 bits
  2. 192 bits
  3. 256 bits



Question: How AES encryption works?
AES comprises three block ciphers i.e AES-128, AES-192 and AES-256. Each cipher encrypts and decrypts data in blocks of 128 bits using cryptographic keys of 128-bits, 192-bits and 256-bits, respectively. Secret-key ciphers use the same key for encrypting and decrypting. In this encryption method works three times faster in software than DES.


Question: Write a Function to encrypt/decrypt?
function encrypt_string($string = '', $salt = '8638FD63E6CC16872ACDED6CE49E5A270ECDE1B3B938B590E547138BB7F120EA') {
    $key = pack('H*', $salt);    
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv);
    return base64_encode($iv . $ciphertext);
}

function decrypt_string($encodedText = '', $salt = '8638FD63E6CC16872ACDED6CE49E5A270ECDE1B3B938B590E547138BB7F120EA') {
    $key = pack('H*', $salt);
    $ciphertext_dec = base64_decode($encodedText);
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv_dec = substr($ciphertext_dec, 0, $iv_size);
    $ciphertext_dec = substr($ciphertext_dec, $iv_size);
    return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
}
/** Encoded Decoded with 256 bits * */
$encodedText = encrypt_string('hello');
echo decrypt_string($encodedText);



Question: How to encrypt/decrypt a string using 192 bits?
/** Encoded Decoded with 192 bits * */
$encodedText  = encrypt_string('hello', '8638FD63E6CC16872ACDED6CE49E5A270ECDE1B3B938B590');
echo decrypt_string($encodedText , '8638FD63E6CC16872ACDED6CE49E5A270ECDE1B3B938B590');



Question: How to encrypt/decrypt a string using 128 bits?
/** Encoded Decoded with 128 bits * */
$encodedText  = encrypt_string('hello', '8638FD63E6CC16872ACDED6CE49E5A27');
echo decrypt_string($encodedText , '8638FD63E6CC16872ACDED6CE49E5A27');