Lambneck Posted February 15, 2009 Share Posted February 15, 2009 So I'm trying to figure out how to get the url's of my dynamic web pages to look nicer and be more search friendly. The dynamic pages are based on info in a mysql database. In the database is a row that stores titles of each page. my question is, how to get the titles from the database to be in the url? instead of, for example, /display.php?id=23 etc. also a second question related to the first... since the titles are written with spaces in between each word, how to replace the blank spaces with hyphens? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/ Share on other sites More sharing options...
dreamwest Posted February 15, 2009 Share Posted February 15, 2009 RewriteRule ^Pretty_url/(.*) display.php?id=$1 Will show http://www.site.com/Pretty_url/23 Quote Link to comment https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/#findComment-762636 Share on other sites More sharing options...
Lambneck Posted February 15, 2009 Author Share Posted February 15, 2009 hi, ok thats cool htaccess alteration, but is there a way to get info from mysql database row into the url instead? Like the other part of my original question about selecting the topics title from the database and having that inserted into the url somehow. Is that even possible? Quote Link to comment https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/#findComment-762715 Share on other sites More sharing options...
plodos Posted February 15, 2009 Share Posted February 15, 2009 If your code is working, could you share your .htaccess and .php files? Also i have same problem...I want to see the solution.... Quote Link to comment https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/#findComment-762719 Share on other sites More sharing options...
a-scripts.com Posted February 15, 2009 Share Posted February 15, 2009 hi, ok thats cool htaccess alteration, but is there a way to get info from mysql database row into the url instead? Like the other part of my original question about selecting the topics title from the database and having that inserted into the url somehow. Is that even possible? hello, sure it is possible. It's pretty much same as dreamwest wrote. Just create a htaccess rule like RewriteRule ^Pretty_url/(.*)/(.*) display.php?id=$1 and then you can use urls like http://www.site.com/Pretty_url/23/whatever-topic-title-goes-here with the id of the title accessible by $_GET['id'] in display.php file. If you want to get rid of the ID in url completely you would need to provide unique identifier for each topic .. say My Topic would use my-topic as identifier and you can then change the htaccess rule to something like RewriteRule ^Pretty_url/(.*) display.php?identifier=$1 and then link to http://www.site.com/Pretty_url/my-topic and access the topic by it's identifier $_GET['identifier'] that's equal to 'my-topic' in this case. Just make sure you would use only legal characters in the urls. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/#findComment-762735 Share on other sites More sharing options...
Lambneck Posted February 15, 2009 Author Share Posted February 15, 2009 a-scripts that does help a lot. i a couple follow up questions though. Is there a way to automatically remove illegal characters? and also is there a way to limit the amount of words that get taken from the database? some of the titles are really long and if it were possible to only pass up to a maximum amount using $_GET[id] it would be ideal in this situation. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/#findComment-762795 Share on other sites More sharing options...
bothwell Posted February 15, 2009 Share Posted February 15, 2009 Add another column in your database to hold a url slug. When you do your insert, build the slug with all the conditions that you want to use on it (so use str_replace() to strip characters or words you don't want to show up, and trim the string so it doesn't go over a certain length). You can then call your pages using the slug instead of the title or the id number, and use your rewrite condition as normal Quote Link to comment https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/#findComment-762852 Share on other sites More sharing options...
dreamwest Posted February 15, 2009 Share Posted February 15, 2009 hi, ok thats cool htaccess alteration, but is there a way to get info from mysql database row into the url instead? Like the other part of my original question about selecting the topics title from the database and having that inserted into the url somehow. Is that even possible? No sure what you mean but theres always a simple answer Your hyperlinks should look like : http://site.com/Pretty_url/id number Not: http://site.com/display.php?id=$1 For forms use a redir.php instead of direct <form name="searchForm" action="/redir.php" method="GET"> <INPUT class=search_box tabIndex=1 maxLength=128 name="id" value="" size="31"> <input type="submit" value="Search"> </form> And in redir.php: *This will handle all your str replace stuff so you can add extras to the string $search=$_REQUEST['id']; if($id == '' ) { echo "ERROR!!"; //Or just redirect header("Location: index.php"); }else{ $search= str_replace("-"," ",$search); header("Location: Pretty_url/$search"); } Quote Link to comment https://forums.phpfreaks.com/topic/145275-solved-pretty-dynamic-page-urls/#findComment-763047 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.