Jump to content

Create proper urls for posts


daxguy

Recommended Posts

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/

Link to comment
https://forums.phpfreaks.com/topic/273800-create-proper-urls-for-posts/
Share on other sites

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

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

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?

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...

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/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.