为什么有的网址中不包含“/”、“#”等?

声明:这个问题需要更多的细节或更清晰的表述,因此目前不接受回答。

想改进这个问题吗?通过编辑这篇文章,添加详细信息并表述清晰该问题。

我想知道现在的网站是如何管理它们的内容和Ajax调用的。Facebook怎么的URL为什么是这样子:http://www.facebook.com/zuck,而不是这个样子:http://www.facebook.com/zuck/

这显然会更加方便,因为他们不需要创建一个名为zuck的子目录。

我注意到这个URL也是这样:http://hypem.com/popular 。这很方便,他们能够不间断地运行媒体播放器,因为他们的网址中不需要一个#。

this kind of URL mostly uses the Apache redirect rules in .htaccess files, you will see something like domain.com/example but in the back-end it is really mean redirect this to something like

domain.com/subdirectory/maybe-anotherone/example.php

so they dont want users see the exact pattern of their system.

This is URL rewrite feature.

There might be various implementations, but basically it's done by the web server taking the request path (e.g. /zuck/ or /popular/songs/whatever) and instead of looking for files in that path it parses it to parameters (there might be rules defined in HTTP stack or later on).

So http://www.facebook.com/zuck inside the server becomes an equivalent of http://www.facebook.com/profile.php?id=zuck (or something along those lines).

It's called nice-url's or URL-Rewrite.

You can traverse all requests to the index.php and there you can handle it by $_SERVER['REQUEST_URI'] in PHP.

You need to create a file, named .htaccess at the root of your site. This is the code what I'm using, it keeps images, icons and some other resources out of the scope:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/phpmyadmin/
    RewriteRule !\.(ico|gif|jp?g|png|pdf|doc?|xls?|ppt?)$ index.php [L]
</IfModule>