url rewriting in php | seo friendly url using htaccess rewriterule


Share on Facebook Twitter

1 years ago -   September 11, 2018

Url rewriting is a recommended part of search engine optimization, by doing this we make a clean URL which 
is improve visibility (ranking) in the search engine like google yahoo bing etc. so here we describe how to make
clean SEO friendly URL  by using URL rewriting in PHP

For URL rewrite we use htaccess file in which our web page redirect for spacial SEO URL that we want to see
on URL bar section in our browser. before starting this we must know some key point about htaccess rewriterule 

what is url rewrite
URL rewrite is the process of manipulation of url by which we can make relevant, easily memorable, preferable
for search engine url. in apache server mod-rewrite is a powerful module define which is provide all kind of url 
manipulation that we want. mod-rewrite is successfully redirected to own custom url in web server.

what is htaccess file
.htaccess is a configuration file which run in our server. in this file, we write code for server module like
mode-rewrite. .htaccess file must contain in root directory where our index file is found.its name 
simply start with (.) dot then htaccess so first create .htaccess file and save it in root directory where
index file found 

Let's discuss all cases of that we want to rewrite url in our website..

case 1: simply page rewrite

  www.techifind.com/product_view.php
                      ||
 www.techifind.com/product

Solution:
in htaccess file we have to write given below.in the first line RewriteEngine On simply tell the apache server to
 start mode-rewrite  module.in second line RewriteRule  ^(product+)/?$    product_view.php   [NC,L]
where RewriteRule is simply redirect the content of product_view.php  for  /product.

actually what here done when in the browser we write www.techifind.com/product and press enter then all 
the content of product_view.php will be  loaded and show while in our browser url section is
www.techifind.com/product.here  we can replace  product with any required text  in the second line of
htaccess file the result will be same

.htaccess
  
       RewriteEngine On
       RewriteRule   ^(product+)/?$    product_view.php   [NC,L]
     

case 2.1: page rewrite with one get variable

 www.techifind.com/product_view.php?pid=777           www.techifind.com/product_view.php?pid=deal-of-the-day
                      ||                                                OR                                  ||
 www.techifind.com/product/777                                   www.techifind.com/product/deal-of-the-day

Solution:-
in this case we want to load page www.techifind.com/product_view.php?pid=777  at browser Url www.techifind
.com/product/777. so in .htaccess file we  write RewriteRule  ^product/([a-zA-Z0-9-/]+)$  product_view.php
?pid=$1  [NC,L]  like given in the box here Regex expression [a-zA-Z0-9-/] is indicatethevalue of pid which is
777. this regex expression is true for all string or numerical Get value 

.htaccess
  
       RewriteEngine On
      RewriteRule    ^product/([a-zA-Z0-9-/]+)$        product_view.php?pid=$1  [NC,L]
     


case 2.2: page rewrite with only get variable after website domain name

  www.techifind.com/product_view.php?pid=deal-of-the-day
                      ||
 www.techifind.com/deal-of-the-day

Solution:-
this is the most desirable case of seo friendly url .this kind of url mostly use in blogging website where we 
send dynamic blog id  value with get method and we want this get value appear just after our domain name
like www.techifind.com/deal-of-the-day.basically we want to redirect urlwww.techifind.com/deal-of-the-day
to  www.techifind.com/product_view.php?pid=deal-of-the-day.

important : code line  RewriteRule    ^([A-Za-z0-9-]+)/?$   product_view.php?pid=$1  [NC,L]  is simply
redirect to product_view.php?pid=dynamic-get-value. if we write any string or numeric value after domain
like techifind.com/deal-of-the-day then we see page result of product_view.php?pid=deal-of-the-day. it 
means the string after domain deal-of-the-day is the value of  pid which replace the $1

here some difficulty may be we can face when we have to redirect  multiple pages for different get value 
example in bar section there is about us button for click this we want lo redirect aboutus.php for url in the 
browser techifind.com/about but in htaccsess we already set that for any string after domain name redirect
to product_view.php?pid=any string to overcome this problum here i discuss in case 2.3

.htaccess
  
      RewriteEngine On
      RewriteRule    ^([A-Za-z0-9-]+)/?$       product_view.php?pid=$1  [NC,L]
     


case 2.3: To overcome problem related page rewrite with only get variable after website domain

  www.techifind.com/product_view.php?pid=deal-of-the-day               www.techifind.com/about.php
                      ||                                                                          VS                     ||
 www.techifind.com/deal-of-the-day                                                     www.techifind.com/aboutus

Solution:-
to solve this problem we use multiple rewrriterule like if we want redirect specific page like aboutus.php then
we rewrite RewriteRule   ^(aboutus+)/?$    aboutus.php  this and then rewrite universal rewrite rule  
RewriteRule  ^([A-Za-z0-9-]+)/?$   product_view.php?pid=$1 [NC,L]. for url www.techifind.com/aboutus
it will redirect to aboutus.php and url except /aboutus it will redirect to product_view.php?pid=any_string
_except_aboutus

.htaccess
  
      RewriteEngine On
      RewriteRule   ^(aboutus+)/?$    aboutus.php
      RewriteRule    ^([A-Za-z0-9-]+)/?$       product_view.php?pid=$1  [NC,L]


case 3.1: page rewrite with two get vaiable

    www.techifind.com/product_view.php?pid=deal-of-the-day&p_category=mobile              
                      ||                                                                          
    www.techifind.com/product/deal-of-the-day/mobile                                                 
Solution:-
in this case simply we want to change page name product_view.php to product  then with slash(/)first get 
value after that second get value like in above box. the answer is not so hard if understand above cases.
our rewrite rule isRewriteRule  ^product/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$   product_view.php?yes=$1&no=
$2  [NC,L] where first regex expression point to get value pid=$1 and second regex  exp [A-Za-z0-9-] is
get value pcategory=$2.

.htaccess
  
   RewriteEngine On
   RewriteRule  ^product/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$   product_view.php?pid=$1&p_category=$2  [NC,L]


case 3.2: page rewrite with only two get variable after domain 

    www.techifind.com/product_view.php?pid=deal-of-the-day&p_category=mobile              
                                                ||                                                                          
    www.techifind.com/deal-of-the-day/mobile                                                 
Solution:-  
As above after domain name we want  only both get value like i  discuss in case 2.2 we add &p_category=$2
for  for second get value and second regex expression [A-Za-z0-9-]+  is define variable $2
  
.htaccess
  
      RewriteEngine On
      RewriteRule  ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$        product_view.php?pid=$1&p_category=$2  [NC,L]


Whats new

how to make multiple selection in dropdown ( chosen select ) with select2 and ajax

6 month ago by techifind


how to make hand drawing effect for custom image in videoscribe using illustrator

1 years ago by techifind


how to remove noise from voice using fl studio

1 years ago by techifind

Subscribe to this blog and receive notifications of new posts by email.