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]