Kleidi Posted March 1, 2010 Share Posted March 1, 2010 Hello everyone! I'm playing around with a little script that i'm working from some weeks . 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! Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/ Share on other sites More sharing options...
JasonLewis Posted March 1, 2010 Share Posted March 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1019641 Share on other sites More sharing options...
Kleidi Posted March 1, 2010 Author Share Posted March 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1019653 Share on other sites More sharing options...
JasonLewis Posted March 1, 2010 Share Posted March 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1019655 Share on other sites More sharing options...
Kleidi Posted March 1, 2010 Author Share Posted March 1, 2010 Ok, but variable $page what means? I should replace it with the variable $ndeshje that contains the event title? Sorry for bothering you with this dummy questions! Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1019658 Share on other sites More sharing options...
JasonLewis Posted March 1, 2010 Share Posted March 1, 2010 Yeah, so instead of $page, you'd use the event title variable, which is $ndeshje. So: $permalink = preg_replace("#[^a-z0-9\-]#i", "", str_replace(" ", "_", strtolower($ndeshje))); Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1019660 Share on other sites More sharing options...
Kleidi Posted March 1, 2010 Author Share Posted March 1, 2010 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1019661 Share on other sites More sharing options...
JasonLewis Posted March 1, 2010 Share Posted March 1, 2010 If it was a MySQL error, you should be using mysql_error(); mysql_query($sql, $db) or die('Gabim! Shtimi i ndeshjes deshtoi.<br / > ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1019669 Share on other sites More sharing options...
Kleidi Posted March 1, 2010 Author Share Posted March 1, 2010 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 (_) ! Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1019672 Share on other sites More sharing options...
Kleidi Posted March 2, 2010 Author Share Posted March 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1020597 Share on other sites More sharing options...
Kleidi Posted March 2, 2010 Author Share Posted March 2, 2010 Problem resolved. I have putted at allowed characters and the underline character "_" and everything is resolved now $permalink = preg_replace("#[^a-z0-9\-_]#i", "",str_replace(' ', '_', $ndeshja)); Thank you for your help, anyway! Quote Link to comment https://forums.phpfreaks.com/topic/193719-generate-whitespace-free-urls-for-seo-purposes/#findComment-1020673 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.