Jump to content

First attempt at clean urls need to get title from database


BandonRandon

Recommended Posts

Hello,

 

this is my first attempt at writing my own clean URLs using mod_rewrite. what i currently have is a link like http://bandonrandon.com/news/?action=view&id=61 and i have figured out how to rewrite this as bandonrandon.com/news/view/61 by using

RewriteRule ^news/view/(.+)/?$ news/index.php?action=view&id=$1 [L]

 

however i would like to also have  bandonrandon.com/news/61 do the same thing. i tried

RewriteRule ^news/(.+)/?$ news/?action=view&id=$1 [L]

 

that didn't work.

 

I would ideally like to make the URL something like news/title_of_article but don't know how to pull the title out of the database and into the URL also i read something about adding a "Hash" to the database so the hash would be "title_of_post" instead of title of post. Is this the correct way to do this.

 

Any Help would be great!

  • 3 weeks later...

if you use the first code

the

RewriteRule ^news/view/(.+)/?$ news/index.php?action=view&id=$1 [L]

 

Just change the second part of it to this

RewriteRule ^news/view/(.+)/?$ news/index.php?action=view&title=$1 [L]

 

And get your PHP script to match up the titles.

 

Hope i helped

  • 2 weeks later...

Thanks Paul,

 

I could do that however the problem would be that I believe that the titles would have to be in URL format ie something "site.com/firstword%20secondword%20%thirdword" i could fix this two ways one  by adding a string replace doing something like replace" %20" with "-" or (this is probably what I'll do) add a permalink field to the database what contains what the url should be.

 

Another issue i see with with this is that the if i had 2 post with the same title this could cause an error and do weird crap. the "id" is auto increment meaning that there will never be 2 post with the same id.

 

thanks for your idea that was a good start to helping me understand clean urls. This is for a personal project so I'm not too worried about the time-line.

 

 

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.