Wednesday, May 18, 2016

Mod Rewrite in PHP with .htaccess

What is Mod Rewrite

-> mod_rewrite is used for rewriting a URL at the server level, giving the user output for that final page.
-> mod_rewrite allows us to rewrite URLs in a cleaner fashion, translating human-readable paths into code-friendly query strings.
For example
http://www.yoursite.com/widgets/blue/
http://www.yoursite.com/widgets.php?colour=blue

Set Up mod_rewrite for Apache on Ubuntu

Step 1 — Enabling mod_rewrite

Run The Below Command for activate mod_rewrite.
Øsudo a2enmod rewrite
Note - This will activate the module or alert you that the module is already in effect. To put these changes into effect, restart Apache.
sudo service apache2 restart


Step 2 — Setting Up .htaccess
A .htaccess file allows us to modify our rewrite rules without accessing server configuration files
First, allow changes in the .htaccess file. Open the default Apache configuration file using nano or your favorite text editor.
Øsudo nano /etc/apache2/sites-enabled/000-default.conf 

Inside that file, you will find the
var/www/html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
To put these changes into effect, restart Apache.
sudo service apache2 restart


Step 3 — Create .htaccess file
Now, create the .htaccess file.
Øsudo nano /var/www/html/.htaccess

Add this first line at the top of the new file to activate the RewriteEngine.
/var/www/html/.htaccess
RewriteEngine on

Save and exit the file.
To ensure that other users may only read your .htaccess, run the following command to update permissions.
Øsudo chmod 644 /var/www/html/.htaccess 

You now have an operational .htaccess file, to govern your web application's routing rules.

Step 4 — Setting Up Files
In this section, we will set up a basic URL rewrite, which converts pretty URLs into actual paths to code. Specifically, we will allow users to access example.com/about.
We will begin by creating a file named about.html.

Øsudo nano /var/www/html/about.html 
Ø
Copy the following code into the HTML page.
/var/www/html/about.html
<\html>
    <\head>
        <\title>About Us<\/title>
    <\/head>
    <\body>
        <\h1>About Us<\/h1>
    <\/body>
<\/html>

You may access your web application at your_server_ip/about.html or example.com/about.html. Now notice that only about.html is accessible; if you try to access your_server_ip/about, you will get a Not Found error. We would like users to access about instead. Our rewrite rules will allow this very functionality.

Open up the .htaccess file.
Øsudo nano /var/www/html/.htaccess 

After the first line, add the following.
/var/www/html/.htaccessRewriteRule ^about$ about.html [NC]

Your file should now be identical to the following.
/var/www/html/.htaccess
RewriteEngine on
RewriteRule ^about$ about.html [NC]



Set Up mod_rewrite for Apache on Wamp

Open httpd.conf file from below path

C:\wamp\bin\apache\apache*\conf

Remove # from beginning of
ØLoadModule rewrite_module modules/mod_rewrite.so “

Restart apache server

Note - * Install Wamp Version


Benifits
Applications Must Be Safe

A user must not be able to harm your site in any way by modifying a URL that points to your applications. In order to ensure your site’s safe, check all the GET variables coming from your visitors
For example, imagine we have a simple script that shows all the products in a category.

Generally, it’s called like this:
myapp.php?target=showproducts&categoryid=123
URL Rewrite -> myapp-showproducts-123   

Applications Must Be Search-Engine Friendly

It’s not generally known, but many of the search engines will not index your site in depth if it contains links to dynamic pages like the one mentioned above. They simply take the “name” part of the URL, and then try to fetch the contents of the page.
Ex. myapp-mobile-123

Applications user-friendly

then most of your visitors will find it difficult to get back to their favourite category (eg. Nettools/Messengers) every time they start from the main page of your site. Instead, they’d like to see URLs like this:
http://www.yoursite.com/detail-page-38344.html

Changing html files to php

Sometimes you might have a static html website and need to use php code on the html pages. Rather than redirecting all your html pages to the equivalent php versions you simply need to tell your server to parse html files as if they were php.
AddHandler application/x-httpd-php .html

Error pages

Custom error pages can be set up in cpanel fairly easily, if you want to create a custom error page in htaccess instead use this line:
ErrorDocument 404 http://www.yoursite.com/404.php

Removing query strings 

Some websites like to link to you by adding an query string, for example I could link to www.yoursite.com/index.php?source=blogstorm just so you know where your traffic came from. This creates duplicate content issue for your site so you really need to redirect back to your homepage:
RewriteCond %{QUERY_STRING} ^source= RewriteRule (.*) /$1? [R=301,L] 

Wednesday, November 18, 2015

SQL Case Sensitive Query with Binary Keyword



Make a Case-Sensitive Query


SELECT *  FROM `table` WHERE BINARY `column` = 'value'


BINARY is a built-in keyword that after your WHERE clause that forces a comparison for an exact case-sensitive match. The BINARY operator casts the string following it to a binary string. This is an easy way to force a comparison to be done byte by byte rather than character by character. BINARY also causes trailing spaces to be significant.

As a side note, you can also make columns case-sensitive by adding the BINARY keyword when you create your table.

Wednesday, November 26, 2014

Installing Memcached with WAMP

How to install memcache on wamp server

Follow below instructions
1> The first thing in installing memcached with wamp is to install the Memcached service. download here the Win 32 binary version.
2> Inside the download you will find a memcached.exe file. Extract this to C:\memcached
3> Now to install the service, open a command window and enter the following

- Install the memcached service by running:
C:\memcached\memcached.exe -d install

- Start the memcached service by running:
C:\memcached\memcached.exe -d start

- You can verify if memcached is running by executing this command (You have to see full path of memcached.exe):
C:\memcached\wmic process get description, executablepath | findstr memcached.exe

Install PHP Memcache extension (php_memcache.dll)
- If you don’t have php_memcache.dll in folder C:\wamp\bin\php\php5.x\ext, download it from here and extract into this directory.
- Open php configuration file php.ini from C:\wamp\bin\apache\Apache2.x\bin directory, and add the following line to the end of Dynamic Extensions block which lies between lines 920 and 990 roughly.

extension=php_memcache.dll

- Restart all services of Wamp Server through the system tray menu.

Testing Memcached with PHP

</?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect to memcache");
$version = $memcache->getVersion();
echo "version: ".$version."
";
$tempObject = new stdClass;
$tempObject->strAttr = 'Testing';
$tempObject->intAttr = 0123456789;
$memcache->set('keys', $tempObject, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)
";
$get_result = $memcache->get('keys');
echo "Data from the cache:
";
var_dump($get_result);
?>

Monday, November 24, 2014

How to Upload New Android Apps on Google Play Store

Its very easy to upload or publish Android Apps on google play store
Follow below steps.

1. Generate APK File.
      a. Edit AndroidManifest.xml write (android:debuggable="false") in application tag
              android:debuggable="false" >
      b. Click on File -> Export -> Export Android Application -> Next -> Browse Project -> Next -> Create new keystore and select Location folder and then write File name same to your application name and click on Save -> Write Password and confirm password -> Next
      c. Fill and Write complete form Alias name, password and confirm password same as previous window. Validity years 25. First and Last Name, Organizational Unit, Organization, City or Location, Sate or Province, Country Code like "India". and click Next
      d. Browse location or Destination APK file and Click Finish

2. Create Account and Upload APK for publish Apps
     a. Buy Account on Google play store with $25
     b. Go to your Google Play Developer Console ( https://play.google.com/apps/publish/ )
     c. Near the top of the screen, click Add new application.
     d. Using the drop down menu, select a default language and add a title for your app.
     e. Type the name of your app as you want it to appear in Google Play.
     f. Select Upload APK or Prepare Store Listing to add your app's information.
     g. Browse your APK and upload

3. Compleate fill information which is show in 3 tab
     a. APK
           i. Upload APK file
     b. Store Listing
           i. Fill compleate information which show in form
                  Title, Short description, Full description, At least 2 screenshots and Max 8, screenshots, 7-inch tablet, 10-inch tablet, Hi-res icon, 32-bit PNG (with alpha), Feature Graphic, JPG or 24-bit PNG (no alpha), Application type , Category, Content rating , Website, Email , Privacy Policy and click sate.
     c. Pricing & Distribution
           i. Check countries which you want to live.
4. Select published on drop down right side up corner