Jump to content

Generate whitespace free urls for SEO purposes


Kleidi

Recommended Posts

Hello everyone!

 

I'm playing around with a little script that i'm working from some weeks :P. I'm new on web development. Anyway, i have a script that output from database some text (date, time, event title, event video). Index.php list all the events ordered by date or time and make a link to each of them. When clicks on the link, opens a new page with full informations of the event. The link looks like index.php?id=$id-event=$event where $id and $event are loaded from mysql.

Everything works great but now, i want to make that link SEO friendly, to look like /$id_event_$event (/event_254_premiere_sharlock_holmes). I have created the .htaccess with all needed variables/options but what i need now is the possibility to remove the whitespaces from the event title and replace them with an underscore (_) or something else, bcz, the title of the event in the database is recorded like, ex: Premiere Sharlock Holmes, with whitespaces.

Can someone help me with this issue, PLEASE?

 

my htaccess looks like:

RewriteEngine on
RewriteBase /live/
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^event_([0-9]+)_([a-zA-Z0-9_-]+) index.php?id=$1-event=$2 [NC,QSA,L]

 

Thank you very much!

 

P.S. If i manually put the url on the browser like i need it (/event_254_premiere_sharlock_holmes), it works correctly. What i need now is a way to generate that url on php and to <a href=..> it ;)

Hope that i was understandable!

What you will also need to do, is when entering the data into the database you'll need to make a column called something like 'permalink', which will hold this string, because you also don't want to include special characters.

So basically, to generate the friendly url you use preg_replace() to replace any illegal characters, then str_replace() to replace whitespace with and underscore.

 

$page = "Premiere Sherlock Holmes";

$permalink = strtolower($page); //Turn it all into lower case.
$permalink = str_replace(" ", "_", $permalink); //Replace any spaces with underscores
$permalink = preg_replace("#[^a-z0-9\-]#i", "", $permalink); //Remove any illegal characters, so it can only have numbers and letters.

 

Or you can put it all on one line:

 

$permalink = preg_replace("#[^a-z0-9\-]#i", "", str_replace(" ", "_", strtolower($page)));

 

Hope this makes sense.

Thank you for your reply.

I got the idea, but, how to store the permalink on the db? I use this script to add the event:

<?php
session_start();

if(!isset($_SESSION['loggedin'])) {
   header('Location: '.$domain.'index.php?error=1');
   exit();
}
?>
<?php
include 'D:/Program Files/VertrigoServ/www/live/admini/config.php';

$ndeshja = $_POST['ndeshja'];
$ora = $_POST['ora'];
$data = $_POST['data'];
$data = $_POST['data'];
$kodi = $_POST['kodi'];

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$sql="INSERT INTO ndeshje (`ndeshja`, `ora`, `data`, `kodi` ) VALUES ('$ndeshja', '$ora', '$data', '$kodi')";
mysql_query($sql, $db) or die('Gabim! Shtimi i ndeshjes deshtoi.');

mysql_close();
ob_start();
header('Location: '.$domain.'admin.php?sukses=1');
ob_flush();

?>

...where:

ndeshja - event title

ora - time

data - date

kodi - video source code

 

Thank you very much for your help ;)

You'll need to create a new column in your database called 'permalink' or something along those lines.

Then just use the code I provided above on the event title to generate the url (eg, "this_is_an_event"), then store that variable in the database exactly the same way you've stored the other variables.

Hmmm... i tried it but i got the error that the input was not added to the db. The script looks like this now:

<?php
session_start();

if(!isset($_SESSION['loggedin'])) {
   header('Location: '.$domain.'index.php?error=1');
   exit();
}
?>
<?php
include 'D:/Program Files/VertrigoServ/www/live/admini/config.php';

$ndeshja = $_POST['ndeshja'];
$ora = $_POST['ora'];
$data = $_POST['data'];
$menyra = $_POST['menyra'];
$kodi = $_POST['kodi'];
$permalink = preg_replace("#[^a-z0-9\-]#i", "", str_replace(" ", "_", strtolower($ndeshja)));

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);

$db = mysql_connect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbname,$db);
$sql="INSERT INTO ndeshje (`ndeshja`, `ora`, `data`, `kodi`, `menyra`, `permalink` ) VALUES ('$ndeshja', '$ora', '$data', '$kodi', '$permalink')";
mysql_query($sql, $db) or die('Gabim! Shtimi i ndeshjes deshtoi.');

mysql_close();
ob_start();
header('Location: '.$domain.'admin.php?sukses=1');
ob_flush();

?>

Eh, the error was on the value of the db insert. I forgot to add a variable there. My fault.

I resolved it now, and works great. What i don't like is the way that permalink looks like:

When the title is "Premiere - Sherlock Holmes" in the permalink table is inserted like "premiere-sherlockholmes" instead of "premiere_sherlock_holmes". I need to replace whitespaces and any other "illegal" characters with an underline (_) !

Eh, the error was on the value of the db insert. I forgot to add a variable there. My fault.

I resolved it now, and works great. What i don't like is the way that permalink looks like:

When the title is "Premiere - Sherlock Holmes" in the permalink table is inserted like "premiere-sherlockholmes" instead of "premiere_sherlock_holmes". I need to replace whitespaces and any other "illegal" characters with an underline (_) !

 

Any help, please?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.