Custom URLs to make your website more url friendly

Everybody hates those code filled web URLs, which are hard to remember, and tough to represent. URLs also mean much for Search Engine Optimization (SEO) , I am sure people don’t like to compromise with Search Engine Optimization(SEO) ranks. Well, there is solution for every problem. You can create desired URLs that redirect to any page you want.

How to do?

You will edit .htaccess file such that it redirects every URL to a PHP page, this PHP page will read the entered URL and take you to the right page.

Like this http://wisne.co/this-is-example-page/ to http://wisne.co/?p=this&down=up

Lets start coding

we will edit .htaccess file here,

Write that in a file and save it as “.htaccess” (period “.” as prefix) in the main directory.

what does that code do? That is an apache server configuration code, whenever the URL is entered and queried, the above code checks if the file or destination exists only if the file doesn’t then the server will take it to the handle.php

 NOW, create a MySQL table like one below.

 Name the table anything you want, here I am naming the table as link_s.

Now create a PHP page named handle.php and save it in main directory,


Add the below code the PHP page and save it,

[php]

<?php
$link = $_GET[‘q’];

$connection = mysqli_connect(
“localhost”, /*enter here database servername*/
“shashi”, /*enter mysqli username*/
“asd123”, /*enter mysql password*/
“redir” /*enter here database name */);

if(!$connection){
“Could not connect to the server “.mysqli_errno();
die;
}

$query = ” SELECT * FROM link_s WHERE link = ‘$link’ ” ; #query that is to be executed

$result = mysqli_query($connection,$query);

if(mysqli_affected_rows($connection) == 0 ){
echo “<html><head><title>Error</title></head><body><h1> ERROR ! Page could not be found </h1></body>”;
die;}
$get_it = mysqli_fetch_array($result,MYSQLI_BOTH);

$dest = trim($get_it[‘dest’]);

include(“$dest”);

?>

[/php]

Now, create  link_creation.php to create links.

[php]

<html>
<body>
<?php
if(isset($_POST[‘submit’]))
{
$link  = urlencode(str_replace(‘ ‘,’-‘,$_POST[‘link’]));
$dest = $_POST[‘dest’];

$connection = mysqli_connect(
“localhost”, /*enter here database servername*/
“username”, /*enter mysqli username*/
“password”, /*enter mysql password*/
“database” /*enter here database name */);

//first we will check if the newly requested url already exists

$sql = ” SELECT * FROM link_s WHERE link = ‘$link’ “;

$query = mysqli_query($connection,$sql);

//if link already exists

if(mysqli_affected_rows($connection)!= 0 )
echo “<div> Link Already Exists </div>”;

//if link doesn’t exits then proceed to create link

else
{
$sql = ” INSERT INTO link_s(link, dest) VALUES (‘$link’,’$dest’) “;

$query = mysqli_query($connection,$sql);

}

mysqli_close($connection);

}
?>

<div id=”wrapper”>
<form action=”” method=”post”>
http://yourdomain.com/<input name=”link” type=”text” value=”Enter the URL you want to display” size=”90″><br>
http://yourdomain.com<input name=”dest” type=”text” value=”Enter destination it must reach” size=”90″><br>
<input name=”submit” type=”submit”>
</form>
</div>

<table width=”100%” border=”1″>
<tr>
<td>Link</td>
<td>Destination</td>
</tr>
<tr>
<?php

$connection = mysqli_connect(
“localhost”, /*enter here database servername*/
“username”, /*enter mysqli username*/
“password”, /*enter mysql password*/
“database” /*enter here database name */);

$sql = ” SELECT * FROM link_s “;
$result = mysqli_query($connection,$sql);

while ($row = mysqli_fetch_array($result, MYSQL_BOTH)) {
printf (“<tr><td> %s </td> <td> %s </td></tr>”, $row[0], $row[1]);}

$query = mysqli_query($connection,$sql)

 

?>

</table>

</body>
</html>

[/php]