hide id with mod_rewrite

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

hide id with mod_rewrite

Post by dotphp »

Hi.

I have a link like this:
index.php?page=article-title&id=12

article-title and id are dynamical

How can I show in my address bar only: http://www.website.com/article-title/
not http://www.website.com/article-title/12/

What I must change in htaccess for this.

Thanks
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: hide id with mod_rewrite

Post by Jade »

I'm not an expert in mod_rewrite but I don't think you can. How else would you know which article to load unless you're storing it in a session? Also, if someone was to share the link with a friend how would the page know which ID to pull?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: hide id with mod_rewrite

Post by flying_circus »

dotphp wrote:Hi.

I have a link like this:
index.php?page=article-title&id=12

article-title and id are dynamical

How can I show in my address bar only: http://www.website.com/article-title/
not http://www.website.com/article-title/12/

What I must change in htaccess for this.

Thanks

This is not a problem to be solved with .htaccess, it's better handled by your request router.

If you know that all of your articles have a unique title, the simply pull the data from the database by querying the title, not the id.
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

Re: hide id with mod_rewrite

Post by dotphp »

If I fetch in databases using 'article-title' instead the id it will take longer to search for it ?
I know that if you search by id is much faster than text.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: hide id with mod_rewrite

Post by flying_circus »

dotphp wrote:If I fetch in databases using 'article-title' instead the id it will take longer to search for it ?
I know that if you search by id is much faster than text.
Best practice is to always create an index on commonly searched rows. If you are storing your article-title in a blob or text field, then it will likely take longer, but if it is stored in a reasonably sized varchar field, the performance difference would likely go undetected, that is, unless you have millions of article-titles.

I'm not a SQL expert by any means, I know enough to be dangerous. Maybe someone who has more experience in this field will chime in, but my suggestion is to index the search field.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: hide id with mod_rewrite

Post by pickle »

Why do you want to hide the ID? What do you gain?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply