Creating SEO friendly URLs with PHP and SQL
Creating custom URL or SEO friendly URLs are important if you have a dynamic website and want to get more exposure in search engine results. If you use any CMS service like WordPress, Blogger or Drupal etc they provide service in their CMS to create custom URLs but if you manage your website by yourself and don't have any plugin or service to create custom URLs then you can create your own.
When you want to change the URL from the original dynamic URL your server will be accessing the original dynamic URL but rewrite a custom URL for users which you will specify. So first of all to let server rewrite URLs you need to enable mod_rewrite in Apache settings.
Now you will create .htaccess file in root directory. Many programmers rewrite all the URLs in .htaccess file with some basic knowledge but mostly users found it confusing and complicated. We will use .htaccess only to redirect all the URLs to one PHP page and then rewrite all the URLs from that page with PHP and SQL.
-f is regular file treats string as pathname and tests if it exists.
-d is directory treats string as pathname and tests if it exists.
Then redirects it to the page URLRedirect.php
When you want to change the URL from the original dynamic URL your server will be accessing the original dynamic URL but rewrite a custom URL for users which you will specify. So first of all to let server rewrite URLs you need to enable mod_rewrite in Apache settings.
Now you will create .htaccess file in root directory. Many programmers rewrite all the URLs in .htaccess file with some basic knowledge but mostly users found it confusing and complicated. We will use .htaccess only to redirect all the URLs to one PHP page and then rewrite all the URLs from that page with PHP and SQL.
Create URL Table in Database
CREATE TABLE url ( id INT PRIMARY KEY AUTO_INCREMENT, blogId INT (11), url TEXT, );
Add URL in table
Now where you are inserting your blog or article into database table at same time submit title of your blog in url field of url table. For example if you are submitting your blog with field name Add blog and we consider blogId as id of blog now you can use your blog title as url or add another field and use that for your URL. Now our function becomes like thisif($submit == "Add Blog") { //Add rest of fields in your blog table //Our URL will be in lower case and add dashes instead of spaces, colon marks etc $title = str_replace(" ","-",strtolower($title)); $title = str_replace('"','',strtolower($title)); $title = str_replace("'","",strtolower($title)); $title = str_replace(" & ","-",strtolower($title)); $title = str_replace("&","-",strtolower($title)); $title = str_replace(" ? ","-",strtolower($title)); $title = str_replace("?","-",strtolower($title)); $title = str_replace(":","",strtolower($title)); $title = str_replace(",","",strtolower($title)); mysqli_query("insert into url(blogId ,url) values('$id','$title')"); }
Create .htaccess file
You have added your blog in blog table of database and added URL in url table with blogid. Now we will create .htaccess file and add following lines.RewriteEngine On RewriteBase /rootDirectoryName RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ URLRedirect.php [L]In this file we write some conditions. REQUEST_FILENAME determines full local file path or script matching request.
-f is regular file treats string as pathname and tests if it exists.
-d is directory treats string as pathname and tests if it exists.
Then redirects it to the page URLRedirect.php
Creating URLRedirect.php
Create PHP page, name it URLRedrect.php and save it in the root directory. Add following lines in page.$requesturi = $_SERVER['REQUEST_URI']; $urlinitial = explode("?",$requesturi); $requesturi = $urlinitial[0]; $sql = mysqli_query("select * from urls where url = $requesturi "); $row=mysqli_fetch_array($sql); $bid = $row['blogId']; if($bid) { include("blogdetail.php"); }else{ include("404page.php"); }
Access URL form web
Now we will access this URL form web page. Where you want to give URL add anchor tag and in href call the following functionaccessURL($blogid , "blogdetail.php");We will write function for
accessURL()
function accessURL($id , $url){ $sql = mysqli_query("select * from urls where blogId = $id "); $row=mysqli_fetch_array($sql); return $row['url']; }

Abida
Abida is web developer works with HTML5, Javascript, PHP. She has developed many websites and now shares her knowledge and tips with Knowledge Aspire.
Get latest updates in your inbox
Popular Posts
How Yoga Nurtures Your Programming Skills
Understanding and analyzing basics of SEO
Creating SEO friendly URLs with PHP and SQL
Adobe AIR - Developing cross platform applications
Writing functions to insert, update, search, delete from SQL tables in PHP
Getting started with C#
Creating XML sitemap with PHP and SQL and submitting to search engines
Creating RSS feed in xml format with PHP and SQL
Creating thumbnails dynamically from images with PHP
Backup SQL database with PHP
Tags
CSV
Validation
Android
Java
Nashorn
PHP Mailer
cPanel
Facebook
Marketing
AdSense
Google
Technorati
Feathers
Starling
crop
PDF
Yoga
feedburner
rss feed
meta tags
SQL
backup database
Adobe Air
XML Sitemap
Syntaxhighlighter
Uploadify
Tinymce
screenshot
Text image
webmaster tools
Verifying website
web analysis
SEO
visual studio 2013
IDE