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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by PFMaBiSmAd
removed formatting so that url is readable
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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