Reputation: 127
I am a PHP programmer (newbie). I really admire the way Twitter works with its usernames using such a dynamic URL - http://twitter.com/username
How does it manage to do so? Even Quora and SO work in a similar fashion. I know the get method and then design a template and import data using sessions.
But, how does this dynamic URL thing work?
Upvotes: 1
Views: 551
Reputation: 3345
Whenever you see a url with #! ( known as hash-bang ) then the page that your seeing is being generated via javascript and ajax calls to the server to return all the data that is required.
In Twitter's case when their server receives a url of twitter.com/username there is a url rewrite script that redirects the browser to twitter.com/#!/username which is just the root of the site. The javascript on that page then gets the part of the url after the !# and is coded to display the correct information.
I would also recommend reading http://isolani.co.uk/blog/javascript/BreakingTheWebWithHashBangs as to where this method came from and why some consider that it is actually not a good idea.
Upvotes: 0
Reputation: 11
.htaccess with url rewriting is not enough because all the URI part after the pound (sharp) sign will not even sent to the server by the browser (because it is originally meant to be used locally to locate an anchor inside the document) so it cannot be retrieved by use of a server variable when coming from a direct link (Twitter writes its links directly into its own format in the html page).
So other than rewriting url, you need inside your index page (when the pound sign is not present, meaning that is a direct request) use AJAX to retrieve from the browser, by use of Javascript, the URI fragment after the pound sign, parse it and then push the related content to the visitor.
Upvotes: 1
Reputation: 490423
Whilst Twitter is not written with PHP, the same effect can be acheived.
Using .htaccess
or httpd.conf
(if using Apache), you can add a rule.
RewriteEngine on
RewriteRule .* index.php/$0 [PT]
You can then access that URL to decide where to route the request by looking at $_SERVER['REQUEST_URI']
.
Upvotes: 1