daxguy Posted January 29, 2013 Share Posted January 29, 2013 I was wondering how can I create clean urls using PHP. Can someone explain this to me in laymans terms? Here is my current url a element link and how it looks in the browser http://www.example.com/members/1/posts/page.php?aid=123 But I want it to read the pages title. http://www.example.com/members/1/posts/title-of-current-page/ Quote Link to comment https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/ Share on other sites More sharing options...
Jessica Posted January 29, 2013 Share Posted January 29, 2013 Use mod rewrite. There's lots of tutorials you can google for Quote Link to comment https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/#findComment-1409024 Share on other sites More sharing options...
requinix Posted January 29, 2013 Share Posted January 29, 2013 Keep in mind that you can't do exactly as you've posted: where is the 123 (for the aid) coming from? All you have to work with is the requested URL. However rewriting from /members/1/posts/123-title-of-current-page/ is an option, and so is rewriting to /members/1/posts/page.php?atitle=title-of-current-page Quote Link to comment https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/#findComment-1409026 Share on other sites More sharing options...
daxguy Posted January 30, 2013 Author Share Posted January 30, 2013 I have no clue of working with mode_rewrite().. what should be the rule in the .htaccess file for converting this url to url of my post title i am adding the url here which i want to convert http://uaecarmarket.org/read_news.php?id=30 i want to change this url to my post title.. how shud i do that for instance my post title is present in $post_title? can anyone help Quote Link to comment https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/#findComment-1409216 Share on other sites More sharing options...
requinix Posted January 30, 2013 Share Posted January 30, 2013 URL rewriting lets you move things around in the URL. It cannot just come up with information from scratch: if your read_news.php requires the ID number then it has to be somewhere in the rewritten URL. You can put the title in there too but that can be ignored. http://uaecarmarket.org/news/30-wheelsandmore-pushes-the-ferrari-ff-near-its-tuning-limits See the 30 in there? It doesn't have to be there but it does need to be somewhere. The alternative is editing read_news.php so that it can use a title, but the title has to be unique. Using an ID number is perfectly fine so I recommend you use it. So question: what do you want the URL to look like? Quote Link to comment https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/#findComment-1409220 Share on other sites More sharing options...
Jessica Posted January 30, 2013 Share Posted January 30, 2013 http://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/#entry1409023 Please read the answers you're given and stick to one thread. Quote Link to comment https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/#findComment-1409221 Share on other sites More sharing options...
PFMaBiSmAd Posted January 31, 2013 Share Posted January 31, 2013 You create the 'clean' URL by producing that URL in your code. Typically this is done by writing a function that takes the normal URL, produces the 'clean' URL based on the rules you have defined, and returns the clean URL to be output where needed - function _make_clean_url($url,$title){ $arr = parse_url($url); $arr['path'] = dirname($arr['path']); parse_str($arr['query'], $output); // $output['aid'] is the 123 value // produce the 'clean' url - $url = $arr['scheme'] .'://'. $arr['host'] . $arr['path'] .'/'. $output['aid'] . '-' . str_replace(' ','-',$title) . '/'; return $url; } // example usage $post_title = "title of current page"; $normal_url = "http://www.example.com/members/1/posts/page.php?aid=123"; // note: the use of htmlentities() in the following is for demo purposes and would not be used when outputting an actual url echo htmlentities(_make_clean_url($normal_url,$post_title)); And since this seems to be a duplicate thread, merging the two threads... Quote Link to comment https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/#findComment-1409261 Share on other sites More sharing options...
daxguy Posted February 1, 2013 Author Share Posted February 1, 2013 (edited) Thanks for your replies.. i loved the function given out here it works just fine wid me.. what rule should i be using in .htaccess file to rewrite the following link I am able to make seo friendly urls using the functions.. how shud i implement it now on the website http://uaecarmarket....e_car.php?id=57 to http://uaecarmarket....-2007-For-Sale/ Edited February 1, 2013 by PFMaBiSmAd removed formatting so that url is readable Quote Link to comment https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/#findComment-1409571 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.