隐藏页面扩展(如 StackOverflow)
我想像 stackoverflow 那样隐藏页面扩展。以下是如何工作的?
http://stackoverflow.com/tags/foo
http://stackoverflow.com/tags/bar
我见过很多网站都这样做,但我仍然不知道这是如何完成的(我有一个 LAMP 堆栈)。
I want to hide page extensions like stackoverflow does. How does the following work?
http://stackoverflow.com/tags/foo
http://stackoverflow.com/tags/bar
I've seen a lot of sites that do this, but I still don't know how this is accomplished (I have a LAMP stack).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Apache+PHP 下有几种方法可以做到这一点,但基本原则是对一组 URI(也许是所有 URI,具体取决于您的站点,但您可能需要不同的脚本来处理站点的不同部分)进行翻译到单个 PHP 文件,该文件被告知用户请求了哪个对象。
概念上最简单的方法是将每个 URL 重写为脚本,该脚本通过 获取 URI
$_SERVER['REQUEST_URI']
并按需要解释它。URI 重写可以使用多种方法完成,包括 mod_rewrite、mod_alias 和 ErrorDocument(请参阅 Apache 文档)。
另一种方法是设置更复杂的 URL 重写(可能使用 mod_rewrite)以将路径添加为 GET 变量。
还有
$_SERVER['PATH_INFO']
变量,该变量加载了路径中不存在的部分。此选项几乎不需要修改 Apache 配置文件,但会稍微降低 URL 的灵活性。There are a couple of ways to do it under Apache+PHP, but the essential principle is to make a set of URIs (perhaps all URIs, depending on your site, but you may want different scripts to handle different portions of the site) translate to a single PHP file, which is told what object the user has requested.
The conceptually simplest way is to rewrite every URL to a script, which gets the URI through
$_SERVER['REQUEST_URI']
and interprets it as it likes.The URI rewriting can be done with various methods including mod_rewrite, mod_alias and ErrorDocument (see Apache docs).
Another way is to set up more complex URL rewriting (probably using mod_rewrite) to add the path as a GET variable.
There is also the
$_SERVER['PATH_INFO']
variable which is loaded with the non-existent portion of the path. This option requires little or no modification to Apache config files, but reduces the flexibility of your URLs a little.现代 Web 开发框架支持优雅的 url。查看 Django 或 Ruby on Rails。
Modern web development frameworks have support for elegant urls. Check out Django or Ruby on Rails.
如果您使用 Apache 并且您只是想隐藏静态 HTML 文件的文件扩展名,您可以使用此
.htaccess
代码:Apache
mod_rewrite
已被称为“voodoo,但是非常酷的巫毒”。我在一些网站上使用的实际
.htaccess
代码与此类似,但并不完全相同:这里有一些更长但更具可读性的代码,可以在 Zeus 服务器上执行相同的操作。在 Zeus 上,它被称为
rewrite.script
。If you're using Apache and you simply want to hide the file extensions of static HTML files you can use this
.htaccess
code:Apache
mod_rewrite
has been called "voodoo, but seriously cool voodoo".The actual
.htaccess
code I use on a few sites is like that, but not identical:And here is some much longer but far more readable code to do the same thing on a Zeus server. On Zeus, it's called
rewrite.script
.当 Web 服务器收到 URL 请求时,它必须决定如何处理它。经典方法是将 URL 的头部映射到文件系统中的目录,然后让 URL 的其余部分导航到文件系统中的文件。因此,URL 具有文件扩展名。
但没有必要这样做,而且大多数新的 Web 框架也不需要这样做。它们让程序员定义如何将 URL 映射到要运行的代码,因此不需要文件扩展名,因为没有单个文件提供响应。
在您的示例中,没有包含文件“foo”和“bar”的“tags”目录。 “tags”URL 映射到使用 URL 的其余部分(“foo”或“bar”)作为针对标签数据数据库的查询中的参数的代码。
When a web server gets a request for a URL, it has to decide how to handle it. The classic method was to map the head of the URL to a directory in the file system, then let the rest of the URL navigate to a file in the filesystem. As a result, URLs had file extensions.
But there's no need to do it that way, and most new web frameworks don't. They let the programmer define how to map a URL to code to run, so there's no need for file extensions, because there is no single file providing the response.
In your example, there isn't a "tags" directory containing files "foo" and "bar". The "tags" URL is mapped to code that uses the rest of the URL ("foo" or "bar") as a parameter in a query against the database of tag data.
您想要的是干净的 URL,您可以使用 apache 和 .htaccess 来做到这一点。可能有更好的方法,但我一直在这样做:
http://evolt.org/Making_clean_URLs_with_Apache_and_PHP
What you want is clean URLS and you can do it with apache and .htaccess . There may be a better way, but here's how I have been doing it:
http://evolt.org/Making_clean_URLs_with_Apache_and_PHP
这就是 ASP.NET MVC 的魅力和工作。
没有“隐藏”——这只是 ASP.NET MVC 处理 URL 并将这些“路由”映射到控制器类上的控制器操作的方式。
与“经典”ASP.NET Webforms 的做事方式相去甚远。
That's the beauty and the work of ASP.NET MVC.
No "hiding" - it's just the way ASP.NET MVC handles URL's and maps those "routes" to controller actions on your controller classes.
Quite a big step away from the "classic" ASP.NET Webforms way of doing things.