faking .htm at the end of every URL while passing variables

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
yeehawjared
Forum Newbie
Posts: 5
Joined: Tue Dec 19, 2006 11:26 pm

faking .htm at the end of every URL while passing variables

Post by yeehawjared »

I'm building a site similar to http://www.ultimate-guitar.com. Let's digest a sample URL from their site:

http://www.ultimate-guitar.com/tabs/l/led_zeppelin/stairway_to_heaven_ver4_tab.htm

I can assume the variables getting passed are something like:
?artistLetter=l
?artist=led_zeppelin
?title=stairway_to_heaven
?version=_ver4
?type=_tab
.htm

I can emulate this via mod_rewrite up to the point where they have "stairway_to_heaven_ver4_tab.htm" I don't understand how they are keeping these variables separate without something like a / (forward slash) between them. I also don't know how they tack on a .htm on the end of every URL

here is my .htaccess file (note: I don't care about separating out the artistLetter as ultimate-guitar has done. root.php figures out what do do with the variables)

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ root.php?artist=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ root.php?artist=$1&title=$2 [L]
</IfModule>

does anyone know how to generate this fake URL and retrieve the variables inside it (title, version, and type) while adding a .htm at the end?

Many thanks guys :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Why emulate the poor design decisions of another site when you could try to make your own poor design decisions? :P

Seriously though, as long as you're building it, make it REST-ful! It will make things easier on you in the long run (I promise).

Cheers,
Kieran
yeehawjared
Forum Newbie
Posts: 5
Joined: Tue Dec 19, 2006 11:26 pm

Post by yeehawjared »

really? I don't think it's that poor of web design. I'm still really curious about the .htm on the ends of URLS - makes for a clean appearance.

also, I think this thread may help lead me in the right direction...
viewtopic.php?t=50794
yeehawjared
Forum Newbie
Posts: 5
Joined: Tue Dec 19, 2006 11:26 pm

Post by yeehawjared »

figured out the how to fake .htm AND how to pass variables separated by underscores.

http://www.avengex.com/tutorials/188/
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

(short) definition: http://en.wikipedia.org/wiki/Clean_URLs#Clean_URLs

definition of REST: http://en.wikipedia.org/wiki/REST

Cheers,
Kieran
yeehawjared
Forum Newbie
Posts: 5
Joined: Tue Dec 19, 2006 11:26 pm

Post by yeehawjared »

thanks for the links... yeah the whole purpose of this is to create clean URLs.

instead of test.php?artist=jared&title=blah&type=tab&version=1 I'm trying to do:

http://www.test.com/jared/blah_tab_ver1.htm

after finding that tutorial I'll be able to do it. That would be a RESTful approach right?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Don't stop at just removing the GET variables...It would be most restful to do away with filenames all together. Part of the idea is to make your site a resource with an API, rather than a directory of files.

Try to structure your URLs conceptually, like

Code: Select all

http://site.com/artists/(artist)
http://site.com/artists/(artist)/albums
http://site.com/artists/(artist)/(album)
http://site.com/artists/(artist)/(album)/buyAtAmazon
http://site.com/albums/(album)/buyAtAmazon
http://site.com/tabs/(songname)
http://site.com/tabs/(songname)/(version)
http://site.com/tabs/(songname)/(version)/print
http://site.com/users/(username)
http://site.com/users/(username)/message
http://site.com/users/(username)/edit
http://site.com/users/(username)/contributions
http://site.com/search/(term)
http://site.com/browse/(filter)
Generally, this is WAY more readable, as well as more flexible.

You would essentially be making it /(type_of_page)/(specifier)/(action)

It's also easier to code this way, as relative links are a lot simpler. Imagine on every TAB page, you could simply link to "print" or "save" or "send" and the song identifier is already in the URL.

Now THAT's RESTful!

Cheers,
Kieran
yeehawjared
Forum Newbie
Posts: 5
Joined: Tue Dec 19, 2006 11:26 pm

Post by yeehawjared »

yeah that kind of structure is what i'm shooting for.

I did get everything working the way I wanted it to:

//test.php:
<?
echo $_GET["artist"] . "<br>";
echo $_GET["title"] . "<br>";
echo $_GET["type"] . "<br>";
echo $_GET["version"] . "<br>";
?>

//.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)/(.*)_(.*)_ver(.*).htm$ test.php?artist=$1&title=$2&type=$3&version=$4 [L]
</IfModule>


if the input URL is: http://localhost/jared/yellowcard/ocean ... b_ver2.htm

artist = yellowcard
title = ocean_avenue
type = tab
version = 2

I still have to add more RewriteRule conditions, but for the most part I've answered all my questions in the first post. Thanks guys.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Considering I am a Zend Framework head, I enjoy their use of handling urls.. Simply put, all requests will be sent to a front controller, in your case a page controller, and let php parse what is happening in the url.

So in your case,

http://localhost/pagename/namevalue/pai ... pairvalue1

would be parsed as pagename.php?namevalue=pairvalue&namevalue1=pairvalue1
Post Reply