1

How does YouTube make folders dynamic? For example you can access people's channels in the root directory, like:

http://youtube.com/PewDiePie

Making a new folder for each channel is most obviously not how they do it, and they also have links like:

http://youtube.com/PewDiePie/about

Is this done with htaccess, Apache, or is it something with PHP that I should ask on StackOverflow?

  • Though for me the 2nd link does not work (I get a 404 error) – Drav Sloan Aug 21 '13 at 23:31
3

It's called URL re-writing. You can do that in Apache with RewriteEngine, either directly in Apache's configuration, or with .htaccess (there's really no difference). And you can do that on many other web servers (I like nginx for its performance and its ease of configuration).

When you re-write the URL, you assign the dynamic parts of the URL (in your example, whatever is after / and before the second / is one of the dynamic parts), to a variable, passed to some server side language (could be PHP, JSP, doesn't matter). Then on the server side language, this is a regular GET parameter, which you can process - read relevant data from a Database, and so on.

For example, you rewrite:

^(.*)/(.*)$ /controller.php?user=$1&page=$2

Then in PHP, the code:

echo $_GET['user'];

Would print:

PewDiePie
1

These aren't really physical directories on the system. They're dynamic names within the URL name space that get mapped to either a physical directory or are interpreted as an argument to a function or application which then acts on it.

It's typically URL rewriting that's facilitating this "magic". Take a look at this tutorial from smashingmagazine if you're really curious, titled: Introduction To URL Rewriting.

Example

Say you're wikipedia. The entire site is primarily PHP based so the real URLs are something like this:

http://en.wikipedia.org/w/index.php?title=Barack_obama

But when you're at the site you're likely to see the page like this:

http://en.wikipedia.org/wiki/Barack_obama

The Apache configuration that makes this happen:

RewriteEngine On
#Look for the word "wiki" followed by a slash, and then the article title
RewriteRule   ^wiki/(.+)$   w/index.php?title=$1   [L]

This takes the URL that the browser is accessing "http://en.wikipedia.org/wiki/Barack_obama" and rewrites it internal to the web server (Apache) to this, "http://en.wikipedia.org/w/index.php?title=Barack_obama".

I often think of this as similar to on Star Trek where they had the universal translator. The server is taking one set of URLs on the input (from the browser) and on the backside is translating those URLs into other ones which map to actual applications and scripts on the server or even other servers.

  • Note using a RewriteRule without reverse proxying will also rewrite the URL in the location bar too. Hence the suggestion of AliasMatch so it behaves like the examples given by the OP :) – Drav Sloan Aug 21 '13 at 23:49
  • @DravSloan - thanks for adding that, I was just giving them the basics, I have the feeling since they didn't know about this feature/facility of web serving, that AliasMatch and all the rest of the features will go over their head. – slm Aug 21 '13 at 23:53
1

To simply redirect all urls that do not have a / within them, you could achieve with a simple AliasMatch (from mod_alias):

AliasMatch ^/([^/]+) /path/to/handler.php

You could then look at the REQUEST_URI environment variable in your script to process the user to display.

And then for additional stuff like /about handling:

AliasMatch ^/([^/]+)/(about|videos|blah) /path/to/handler.php

or you could get it to match just two levels (so paths like /x/y/z will not be handled) in the same way we matched the user part above ([^/]+ one or more characters that are not a slash):

AliasMatch ^/([^/]+)/([^/]+) /path/to/handler.php

And again use the REQUEST_URI to work out what to display.

This will serve the page URL "as-is" (so the url stays the same on your location bar in your browser) yet the content will be generated by /path/to/handler.php.

These would be two separate rules. There would probably be a way to handle just a /username and /username/something in one rule, but then it would most likely end up being quite unreadable. They are done in this fashion to avoid matching everything after domain.com.

If you need to change to path into arguments to the script, then you are into the land of RewriteRule (from mod_rewrite). If you want to maintain the URL in the location bar you will have to do a reverse proxy query:

 RewriteEngine On
 RewriteRule ^/([^/]+) /path/to/handler.php?user=$1 [LP]
 RewriteRule ^/([^/]+)/(about|videos|blah) /path/to/handler.php?user=$1&args=$2 [LP]

This would save the script having to process REQUEST_URI, and can pull variables from $_GET['user'] and $GET['args'] if using PHP.

Again, like AliasMatch that second rule could be:

 RewriteRule ^/([^/]+)/([^/]+) /path/to/handler.php?user=$1&args=$2 [LP]

but note for both of these, your script will have to know how to handle everything that matches two urls. This may not be what you want for such urls as:

 /errors/404.html
 /about/google.html

and so on.

  • Updated my answer as AliasMatch cannot do script arguments, you would have to use RewriteRule from mod_rewrite instead. AliasMatch can be used, and the script will have to process the REQUEST_URI environment variable instead. – Drav Sloan Aug 22 '13 at 9:15
  • The only issue I see with this one is that I'd like to have pages within that page, like youtube.com/user/PewDiePie/about – rtainc Aug 22 '13 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.