fight coronavirus(corvid-19) - As they treat the sick & find the solution, please stay at home + wash your hands every after activity, GOD bless you.
How to create smart Urls using PHP

In this post, you will learn about creating Clean URL’s in PHP without using the Apache htaccess file on your server.

Having clean URLs will enable search engines and other websites to link to your website easily. Due to this, they are very important.

Note: we are only going to clean up the title parameter passed into the url.

Wait,What is a URL?

This is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it.

In short terms,it's the address of a World Wide Web page.

URL in full
  • U - Uniform
  • R - Resource
  • L - Locator

Ok back to the topic, by default when you fetch data from the database it is returned the way it was stored. For this case lets concentrate on the links.


Take note of the title parameter, The actual title is returned back from the database with it's original spaces every after single character.


<a href=" to create a website">
How to create a website

So when users click on such a link, this is how it will be displayed in the browser web address.The title parameter will contain %20 characters which we don't want.


	//title parameter

Good news, with PHP we can strip out those characters(%20) by using the str_replace function.


The str_replace() function replaces some characters with some other characters in a string.


	str_replace(search, replace, subject);

Parameter Description
search Required. Specifies the value to find
replace Required. Specifies the value to replace the value in search.
subject Required. Specifies the string to be searched.

Absorbing this amount of content would be a lot if you are just reading it, So I have put together a very simple video for you to understand the logic.

Project source code

The file you will download contains 3 files,namely;

  • index.php
  • post.php
  • url_convertor.php
This is the index.php file.


 <title>Smart Url</title>

	//includes the file to clean on the title parameter
	require_once 'url_convertor.php';

	//fake data to use
	$id = 100;
	$Article_title = "How to create a website";

	<a href="post.php?id=<?php echo $id; ?>&title=<?php echo UrlConvertor($Article_title); ?>">
		<?php echo $Article_title; ?>

This is the post.php file.

This file is actually pulling the title parameter value from the web address and displaying to the browser.


		$title = $_GET['title'];

		echo $title;


This is the url_convertor.php file.

This is a file that handles all the logic for making our title parameter clean.



	function UrlConvertor($title){

		$url = str_replace(' ', '-',$title);

		return $url;


Since we included the url_convertor.php file in the index.php file. We get access to the UrlConvertor function.

To get a clean url title parameter, we wrap the Article_title from the database inside the UrlConvertor function like this.


&title=<?php echo UrlConvertor($Article_title); ?>

To download the source code: Click here description

Related Post(s)

» Learn PHP Complete Guide - Introduction

» Quick Autocomplete App With PHP and JQUERY MOBILE

» Learn PHP Complete Guide - Environment Setup

» How to create smart Urls using PHP

» How to convert A Timestamp in Date Format

collections_bookmark Category :: Php
date_range Published :: 1 year ago At: 11:45 AM
event_note Detailed Date :: May 15th, 2019
person Writer :: Code