URL Rewriting- Top 5 Ways of PHP URL Rewriting


Introduction

In this era of technology enhancement, Internet has become the major source of information. If we need any kind of information, we usually consult from the Search Engines (particularly Google). Why? Because of the reason, Search Engine’s increased searching capabilities.
Thus every website owner wants now online visibility on top search engines. And for this you need to follow search engine guidelines. These are the guidelines which are helpful in the Search engine optimization of your website.
URL Rewriting is one of the good techniques followed in Search Engine Optimization. Consider more understanding of URL Rewriting and its importance:
Static URLS Vs Dynamic URLS
Static URL’s are more to be recognizable than dynamic URL’s for the number of reasons:
  1. Static URL’s generally rank better in Search Engines.
  2. Search Engines Crawls the Content of the static URLs much faster as compare to the dynamic URL.
  3. Static URL looks pretty or friendlier to the end-user.
Consider the Example of Static URL’s Vs Dynamic URL’s:
Static URL/Pretty URL:
http://www.design.com/web design/custom web design
Dynamic URL:
http://www.example.com/web design.aspx?design=custom&ID=2065
The above mentioned Dynamic URL has the following problems discussed below:
  • When this URL is crawled by the Search engine, it will have very less chance to become on top ranks because the URL do not defines what the page is all about, thus cannot become visible for any keyword.
  • The type of URL exposes the underlying technology; in this case it is Asp.net.  This can give clue to the potential hackers about what data they should send along with query string for a front door attack to the website. So technology extensions should keep hide from the web pages.
  • The type of URL contains the sticky elements like question mark & ampersand. Those special characters included in the URL are problematic in the case when another website owner will link to your website using this URL it can have issues with their xHTML.
  • Some of the Search engines do not crawl or index those pages whose URL’s are dynamically generated.  They’ll see the question mark in the URL and just turned back if they found it.

URL Rewriting Methods:

Readable URL’s are not only demanded by the Search Engines but by the humans as well. The reason is Search Engines are made for human beings not for Machines.
Consider the Example of How URL can be Rewrite for the Search Engines Optimization of your website:
Case to consider:
http://www.yourdomain.com/profile.php?mode=view&u=7
There are two ways by which this dynamic URL can be rewrite:
MethodsDefault FormatAfter Rewriting
Single Page URLhttp://www.yourdomain.com/profile-mode-[VALUE]-u-[VALUE].htmlhttp://www.yourdomain.com/profile-mode-view-u-7.html
Directory Type URLhttp://www.yourdomain.com/profile/mode/[VALUE]/u/[VALUE]/http://www.yourdomain.com/profile/mode/view/u/7/

URL Rewriting in PHP

There are the following requirements for Rewriting the URL in php:
IndexRequirements
1mod_rewrite module must be loaded in apache server.
2Need to enable FollowSymLinks option otherwise you may encounter 500 Internal Server Error.
3After that you need to create a .htaccess file in the root folder of your web directory.
Consider the Five different aspects of URL Rewriting Examples:
1. Rewriting the test.php to test.html
Add the following code into the .htaccess file.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]
After placing the code, when the URL like http://localhost/test.htm is called in address bar it calls the file test.php.
Here:
$1=First regular expression of the part of the RewriteRule
[nc]= not case sensitive
2. Rewriting product.php?id=12 to product/ipod-nano/12.html
In this technique, you can smartly place the keyword in the url. Like in the above mentioned url, ipod-nano is the product name which is placed in the url after rewriting.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3. Redirecting without  www URL to www URL
Sometimes you might have noticed that when you type yahoo.com, you will be redirected to the www.yahoo.com. If you want this type of URL Redirection you can use the following code:
RewriteEngine On
RewriteCond %{ HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4. Rewriting yourdomain /user.php?username=xyz to yourdomain.com/xyz
By the code mentioned below, you can rewrite your URL according to the Method of Directory type as mentioned above in the heading URL Rewriting Methods.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5. Redirecting the domain to a new subfolder of inside public_html.
Consider the case: If you want to redevelop your site, you can place the new site into the folder named new.It means all the files of redeveloped site is placed inside the root directory of folder named new. Here you need to understand that there can be a mess to paste all the files in the root folder (or can be a hectic process), so we don’t have this option. It means now your website can only be assess by “http://www.test.com/new”. So after placing the following code into the .htaccess file , you can access “www.test.com”, whose files are actually placed in the new folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

POPULARITY LEVEL: More than 80 %
Please Leave your Comments About the Post!
Company Profile:
We as a Leading Web Design Company Offers Best and Cost effective Web solutions to our clients in USA, UK and Canada. I being as a Project Manager: Kaitlyn Em is available for your support, you can drop me an email at kaitlyneml​y@aim.com, And can Call Us by visiting our website http://www.cyberdesignz.com/clients/
We implement your needs and requirements with the use of latest web trends and technologies in an effective way.

Comments

  1. realy it help full for rewrite url. thanks share this

    ReplyDelete

Post a Comment

Popular Posta

How to enable .htaccess in wamp server

Web Scraping Amazon with PHP

Disable right click script and Google Instant Map Search