.Darkman Posted March 7, 2007 Share Posted March 7, 2007 Hello Everybody, I am developing a Blog system. I am doing it for two reasons : 1) To Learn more about PHP 2) Wordpress and other systems are too sophisticated for me. I need some help in getting Search Engine friendly URLs. For eg, I have a Post's title as "A New Post". That is entered into one field of the DB. I want to insert into another field as "a-new-post" so that i can use it in URLs. Could anyone advice me how to do it ? Also what can be done if the Title contains some special characters. Please do explain in detail. I am Newbie. Thanks, Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/ Share on other sites More sharing options...
nloding Posted March 7, 2007 Share Posted March 7, 2007 Actually, I'm interested to hear the answer to this. I am unsure of the best way myself! Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-201627 Share on other sites More sharing options...
monk.e.boy Posted March 7, 2007 Share Posted March 7, 2007 This is pretty simple with .htaccess rewrite rules. Apache will look at the URL and then translate it for you, your .htaccess file will look like: Options +FollowSymLinks RewriteEngine on RewriteRule ^blog/(.*).html /index.php\?val=$1 [nc] So what this does is when someone browses: http://www.site.com/blog/my-first-blog.html Apache translates this and calls: http://www.site.com/index.php?val=my-first-blog So then in index.php you can do: <?php echo $_GET['val']; ?> and you get 'my-first-blog', so then you can do: SELECT * FROM my_blog WHERE title LIKE '$_GET['val']' WARNING - SQL injection error in the above code (use mysql_escape_thingy() ) Now search google and post some more comments in this thread!!! http://www.google.co.uk/search?num=100&hl=en&q=.htaccess+rewrite+pretty&btnG=Search&meta= monk.e.boy Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-201633 Share on other sites More sharing options...
monk.e.boy Posted March 7, 2007 Share Posted March 7, 2007 ooops Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-201634 Share on other sites More sharing options...
.Darkman Posted March 7, 2007 Author Share Posted March 7, 2007 Hi monk.e.boy Yeah, i know this. What i wanted is that My Post's title will be "My Blog Post". It will be saved in a field called "title" I have another field "sef_title" I want "my-blog-post" to be stored in "sef_title". How do i do this ? Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-201646 Share on other sites More sharing options...
monk.e.boy Posted March 7, 2007 Share Posted March 7, 2007 <?php $str = "Mary Had A Little Lamb and She LOVED It So"; $str = strtolower($str); echo $str; echo str_ireplace(" ", "-", $str); ?> hows that? monk.e.boy Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-201680 Share on other sites More sharing options...
.Darkman Posted March 7, 2007 Author Share Posted March 7, 2007 Hi, Thanks. That was cool. I think it should be str_replace and not str_ireplace And, Is there anyway by which i can replace all characters (except Alphabets and numbers) with "-" Thanks, Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-201705 Share on other sites More sharing options...
bwochinski Posted March 7, 2007 Share Posted March 7, 2007 <?php $title = preg_replace("/[^\w]+/","-",$str); ?> Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-201742 Share on other sites More sharing options...
.Darkman Posted March 7, 2007 Author Share Posted March 7, 2007 Wow ! That was excellent. thanks, Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-201749 Share on other sites More sharing options...
.Darkman Posted March 8, 2007 Author Share Posted March 8, 2007 <?php $title = preg_replace("/[^\w]+/","-",$str); ?> What does that do actually ? Replace all Non-Alpha Numerics Characters ? Does ^\w stand for those characters ? Thanks, Link to comment https://forums.phpfreaks.com/topic/41613-search-engine-friendly-urls/#findComment-202193 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.