abrahamgarcia27 Posted December 8, 2013 Share Posted December 8, 2013 (edited) I have a question on how to structure my url for a blog. Before i used the ID of the blog_id, but i feel i should add the title of the post (Like they do on this forum). Then i added the the title to the blog to query the blog post, but i came across the problem that the title was similar. I am just trying to get some knowledge from my fellow friend on how to structure a url path for SEO. I use .htaccess for rewriting my urls this is the structure i use. Also some people added characters which broke my query like (;,"'@^, etc...) index.php?action=blogpost&title=$title /blog/post/$blog_title index.php?action=blogpost&blog_id=$blog_id /blog/post/$blog_id Edited December 8, 2013 by abrahamgarcia27 Quote Link to comment https://forums.phpfreaks.com/topic/284618-url-path-question/ Share on other sites More sharing options...
requinix Posted December 9, 2013 Share Posted December 9, 2013 Use both the ID and title. Otherwise you have to make sure the title is unique for every post (which, you know, it kinda should be) but you can do that easily enough by assigning each post a specific "title" just for the URL. As for people breaking your queries, you're doing something very wrong. Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/284618-url-path-question/#findComment-1461700 Share on other sites More sharing options...
abrahamgarcia27 Posted December 9, 2013 Author Share Posted December 9, 2013 Ok this is what i am using for .htaccess RewriteRule ^post/([A-Za-z0-9-]+)/?$ index.php?action=blogpost&title=$1 [NC,L] This is how i send the title to query $urlTitle = strtolower(str_replace(' ', '-', $blogs['title'])); <div class="post-meta-readmore"><a href="<?php echo SITE_PATH; ?>post/<?php echo $urlTitle; ?>">Read more</a> </div> And this is how i query the post function getBlog($title){ $string = str_replace('-', ' ', $title); $db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); $sql = "SELECT * FROM `".TABLE_BLOG."` LEFT JOIN `".TABLE_BLOG_CAT."` ON `".TABLE_BLOG."`.category_id = `".TABLE_BLOG_CAT."`.category_id WHERE title = '$string' LIMIT 1"; $record = $db->query_first($sql); return $record; } Quote Link to comment https://forums.phpfreaks.com/topic/284618-url-path-question/#findComment-1461870 Share on other sites More sharing options...
abrahamgarcia27 Posted December 9, 2013 Author Share Posted December 9, 2013 If some one post a title like the following it breaks the query. The New Era of Music: Indie, Metal, Alternative Quote Link to comment https://forums.phpfreaks.com/topic/284618-url-path-question/#findComment-1461871 Share on other sites More sharing options...
Ch0cu3r Posted December 9, 2013 Share Posted December 9, 2013 (edited) it is most probably the colon that is breaking the url. The colon has a special meaning in urls as it usually defines the http port. Urls should only contain letters, numbers, dashes, underscores and periods. Any other character should be urlencoded. However this will make your urls messy if start encoding non url characters. What you are better of doing is adding another field to your database that stores the url for the page, and the query the database that matches this field instead of the title field. So basically you need to make the entered title url safe. The following will make the title url safe $page_url = str_replace(' ', '-', strtolower($page_title)); $page_url = preg_replace('~[^a-z0-9_\-]+~i', '', $page_url); So if you enter this as the title "The New Era of Music: Indie, Metal, Alternative" it will generate "this-the-new-era-of-music-indie-metal-alternative" as the page url. Edited December 9, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284618-url-path-question/#findComment-1461874 Share on other sites More sharing options...
abrahamgarcia27 Posted December 9, 2013 Author Share Posted December 9, 2013 thank i will try this Quote Link to comment https://forums.phpfreaks.com/topic/284618-url-path-question/#findComment-1461878 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.