how to create custom file name

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
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

how to create custom file name

Post by anand »

I am sure this website uses PHP/MySQL, so my question is, how can it create a custom url like this : -

http://www.mouthshut.com/product-reviews/Kentucky_Fried_Chicken_-Fast_Food_Chain-925009819.html

http://www.mouthshut.com/product-review ... 09819.html

I might sound lame but I really need your advice as I can't figure it out. :banghead: :banghead: :banghead:
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: how to create custom file name

Post by Darhazer »

The quick answer is mod_rewrite and .htaccess ;-)
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: how to create custom file name

Post by anand »

Darhazer wrote:The quick answer is mod_rewrite and .htaccess ;-)

i was thinking the same. but how can it be so specific?

like,

http://www.example.com/kfc-chicken.html
http://www.example.com/apple-macbook-air.html

and, lets say, i start a topic on "2008 beijing world cup" then it'll pull up a page like

http://www.example.com/2008-beijing-world-cup.html
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: how to create custom file name

Post by requinix »

You saw that number at the end of the URL?

http://www.mouthshut.com/product-reviews/What-shows-up-in-here-doesnt-actually-matter-925009819.html - try it

That's how the system knows which article/page you requested. It finds the number at the end, looks up the page according to that number, and displays it. Adding the title in there is just to make it friendlier to people (which has a nice side effect of making it rank better in search engines).

You can do it by title if you want, but it's more of a hassle because you have to check uppercase versus lowercase, converting "invalid" characters to hyphens, and so on.
But the process is the same: you grab the title from the URL, look it up in your database, and figure out which page to display.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: how to create custom file name

Post by mikemike »

As a starting hint, the following in your .htaccess will forward everything to index.php

Code: Select all

RewriteEngine on
RewriteCond $1 !^(index\.php|default\.css|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
 
index.php, default.css, your images folder and robots.txt are ignored.
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: how to create custom file name

Post by anand »

Thanks guys.

Espicially, tasairis
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: how to create custom file name

Post by mikemike »

Charming ;)
Post Reply