Skip to main content

Beginner guide to htaccess - Setup seo friendly url for your website

Here in this tutorial we’ll learn about .htaccess properties and how to setup .htaccess file in our project for custom error pages handling and create seo friendly url.

First of all we need to know what is “.htaccess” ?
htaccess is short for Hypertext Access, and is a configuration file used by Apache based web servers, When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software.


Create filename “.htaccess” and save it to your project root directory.

Note: Before executing this file please make sure that you have successfully enabled mod_rewrite extension in php.ini file

Example-1:
Create your awkward shaped urls into seo friendly urls using .htaccess
Suppose you have these 3 types of urls in your project.



http://www.example.com/profile.php?uid=12345
http://www.example.com/profile.php?username=rohit
http://www.example.com/search.php?q=delhi


Need to convert into seo friendly url like

http://www.example.com/profile/12345
http://www.example.com/profile/rohit
http://www.example.com/search/delhi


.htaccess

Options +FollowSymLinks
RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
 
RewriteRule ^profile/([0-9]+)$ ./profile.php?uid=$1
RewriteRule ^profile/([a-zA-Z0-9_-]+)$ ./profile.php?username=$1
RewriteRule ^search/([a-zA-Z0-9_-]+)$ ./search.php?q=$1

Example-2
Handle two and three parameters


http://www.example.com/profile.php?username=rohit&page=about
http://www.example.com/search.php?q=delhi&page=1&total=100



Need to convert into seo friendly url like
http://www.example.com/profile/rohit/about
http://www.example.com/search/delhi/1/100

For doing this your .htaccess file will be

.htaccess

Options +FollowSymLinks
RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
 
RewriteRule ^profile/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ profile.php?username=$1&page=$2
RewriteRule ^search/([a-zA-Z0-9_-]+)/([0-9]+)/([0-9]+)$ profile.php?q=$1&page=$2&total=$3

This is the just basic and quick understanding tutorial of Htaccess, Uou can learn more from http://www.htaccess-guide.com/

Comments

Popular posts from this blog

How to create a Barcode Using PHP Barcode 128 Generator

A barcode is an optical, machine-readable, representation of data, the data usually describes something about the object that carries the barcode.We will use PHP to generate Barcode in this tutorial. In this script, we are using coding which will generate barcodes in barcode format Code 128 . First, we will create index.php which will ask for the user input for which Barcode has to be created PHP Barcode Generator <fieldset><legend>Detail Informations</legend><form action="createbarcode.php" method="post"><b>Enter Your Code </b><input name="barcode" type="text" /><input type="submit" value="Create Barcode" /></form></fieldset> Now we will create createbarcode.php which will call function from Barcode code128 class for creating barcode <? php include('barcode128.php'); // include php barcode 128 class // design our barcode display echo

Document Expired or Webpage has expired on back button

If you have a dinamic website and want to allow your visitors to use the back button after they sent a form with the post method, the best combination I found was: <?php header("Expires: Sat, 01 Jan 2000 00:00:00 GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); header("Cache-Control: post-check=0, pre-check=0",false); session_cache_limiter("must-revalidate"); // and after you start the sessionsession_start(); ?> I try some combinations using header("Cache-Control: no-cache, must-revalidate"), but when clicking the back button, the last changes in the form back to their previous states. The combination above works fine with IE 6.x. I didn't test this with other browsers. When I try something like session_cache_limiter("nocache, must-revalidate") it doesn't work. The page only updates when I used the browser's refresh button. In dynamic web sites this is not g

How to Enable IMAP PHP in xampp

The imap extension comes as standard with the PHP installation. You just need to enable it in your php.ini   Enable IMAP in XAMPP: You need to configure your php.ini file to enable IMAP extension Search for the line ;extension=php_imap.dll and remove semicolon(;) and restart your xampp. The line should look like as mentioned below. extension=php_imap.dll Note : New php version does not have dll anymore. The default php.ini should already contain a line to load the extension but commented out: ;extension=imap Remove semicolon from above like below extension=imap Enable IMAP in Linux: If you are using LAMP server, First install IMAP using the command on terminal $ sudo apt-get install php5-imap To enable IMAP, run the following command. sudo phpenmod imap Restart apache server with below command sudo service apache2 restart