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!

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

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

 

 

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.