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...

Symbols Used in Excel Formula | The purpose of Symbols in Excel Formula

Following symbols are used in Excel Formula. They will perform different actions in Excel Formulas and Functions. Each of these special characters have used for different purpose in Excel. Symbol Name Description = Equal to Every Excel Formula begins with Equal to symbol (=). Example: = B1+B6 () Parentheses All Arguments of the Excel Functions specified between the Parentheses. Example: =COUNTIF ( B1:B5,5 ) () Parentheses Expressions specified in the Parentheses will be evaluated first. Parentheses changes the order of the evaluation in Excel Formula. Example:   =25+ ( 35*2 ) +5 * Asterisk Wild card operator to to denote all values in a List. Example:   =COUNTIF(B1:B5,” * “) , Comma List of the Arguments of a Function Separated by Comma in Excel Formula. Example:   =COUNTIF(B1:B5 , “>” &C1) & Ampersand Concatenate Operator to connect two strings into one in Excel Formula. Example:   =”Total: “ & SUM(C2:C25) $ Dollar Makes Cell Reference as Absolute in ...

How to Change Currency Symbol Position - Magento 2

The default Magento 2 shows the currency symbol on the left side. However it does not have a feature to change the currency location from left to right.    Programmatic solution to change currency symbol position in Magento 2.   Solution to Change Currency Symbol Position in Magento 2 Create events.xml file at app/code/vendor/Exenstion/etc/frontend   <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">     <event name="currency_display_options_forming">         <observer name="change_currency_position" instance="vendor\Exenstion\Observer\ChangeCurrencyPosition"/>     </event> </config>  Create ChangeCurrencyPosition.php file at app/code/vendor/Exenstion/Observer      <?php namespace vendor\Exensti...