url 重写是如何工作的?
Web服务器如何实现url重写机制并改变浏览器的地址栏?
我不是要求配置 apache、nginx、lighthttpd 或其他的具体信息!
我想知道当服务器想要重写url时,会向客户端发送什么样的信息?
How does web server implements url rewrite mechanism and changes the address bar of browsers?
I'm not asking specific information to configure apache, nginx, lighthttpd or other!
I would like to know what kind of information is sent to clients when servers want rewrite url?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有两种类型的行为。
一是重写,二是重定向。
重写
服务器对自身进行替换,使得像
http://example.org/my/beatuful/page
这样的 URL 被理解为http://example.org/index.php ?page=my-beautiful-page
通过重写,客户端看不到任何内容,并且重定向仅限于内部。浏览器中的 URL 没有变化,只是服务器对它的理解不同。
重定向
服务器检测到该地址不是服务器所需要的。
http://example.org/page1
已移至http://example.org/page2
,因此它使用 HTTP 3xx 代码告诉浏览器新页面是什么是。然后,客户会请求此页面。因此浏览器中的地址发生了变化!流程
该流程保持不变,并通过此图进行了很好的描述:
备注每次重写/重定向都会触发对重写规则的新调用(IIRC 除外),这
对于停止循环非常有用。 (因为当它已经发生一次时它不会重写)。
There are two types of behaviour.
One is rewrite, the other is redirect.
Rewrite
The server performs the substitution for itself, making a URL like
http://example.org/my/beatuful/page
be understood ashttp://example.org/index.php?page=my-beautiful-page
With rewrite, the client does not see anything and redirection is internal only. No URL changes in the browser, just the server understands it differently.
Redirect
The server detects that the address is not wanted by the server.
http://example.org/page1
has moved tohttp://example.org/page2
, so it tells the browser with an HTTP 3xx code what the new page is. The client then asks for this page instead. Therefore the address in the browser changes!Process
The process remains the same and is well described by this diagram:
Remark Every rewrite/redirect triggers a new call to the rewrite rules (with exceptions IIRC)
can become useful to stop loops. (Since it makes no rewrite when it has happened once already).
您是在谈论服务器端重写(例如 Apache mod-rewrite)吗?对于这些,地址栏通常不会改变(除非执行重定向)。
或者你在谈论重定向?这些是通过让服务器响应 HTTP 代码(301、302 或 307)以及 HTTP 标头中的位置来完成的。
Are you talking about server-side rewrites (like Apache mod-rewrite)? For those, the address bar does not generally change (unless a redirection is performed).
Or are you talking about redirections? These are done by having the server respond with an HTTP code (301, 302 or 307) and the location in the HTTP header.
“URL 重写”有两种形式:纯粹在服务器内完成的形式和重定向形式。
如果它纯粹在服务器内部,那么它是内部问题,并且仅与服务器中实现的调度机制相关。例如,在 Apache HTTPD 中, mod_rewrite 可以做到这一点。
如果是重定向,则会在响应中发送表示重定向的状态代码,以及指示浏览器应重定向到哪个 URL 的
Location
标头(这应该是绝对 URL)。mod_rewrite
也可以做到这一点,使用 [R ] 标志。状态代码通常为302(找到),但是它可以配置为其他代码(例如301或307)。
另一个相当常见的用途(通常不会被注意到,因为它在 Apache HTTPD 中通常默认启用)是重定向到目录上带有尾部斜杠的 URL。这是由 mod_dir 实现的:
There are two forms of "URL rewrite": those done purely within the server and those that are redirections.
If it's purely within the server, it's an internal matter and only matters with respect to the dispatch mechanism implemented in the server. In Apache HTTPD, mod_rewrite can do this, for example.
If it's a redirection, a status code implying a redirection is sent in the response, along with a
Location
header indicating to which URL the browser should be redirected (this should be an absolute URL).mod_rewrite
can also do this, with the [R] flag.The status code is usually 302 (found), but it could be configured for other codes (e.g. 301 or 307).
Another quite common use (often unnoticed because it's usually on by default in Apache HTTPD) is the redirection to the the URL with a trailing slash on a directory. This is implemented by mod_dir:
Jeff Atwood 有一篇关于此的精彩文章: http://www.codinghorror.com/blog/2007/02/url-rewriting-to-prevent-duplicate-urls.html
URL重写和转发是完全不同的两件事。 服务器无法控制您的浏览器,因此它无法更改您浏览器的 URL,但它可以要求您的浏览器转到其他 URL。当您的浏览器从服务器收到响应时,完全由您的浏览器决定如何处理该响应:它可以遵循重定向,忽略它,或者非常刻薄地向服务器发送垃圾邮件,直到服务器放弃。服务器没有用来更改地址的“机制”,它只是当特定资源移动到不同位置时服务器遵守的协议(HTTP 1.1),从而产生 3xx 响应。
Jeff Atwood had a great post about this: http://www.codinghorror.com/blog/2007/02/url-rewriting-to-prevent-duplicate-urls.html
URL rewriting and forwarding are two completely different things. A server has no control over your browser so it can't change the URL of your browser, but it can ask your browser to go to a different URL. When your browser gets a response from a server it's entirely up to your browser to determine what to do with that response: it can follow the redirect, ignore it or be really mean and spam the server until the server gives up. There is no "mechanism" that the server uses to change the address, it's simply a protocol (HTTP 1.1) that the server abides by when a particular resource has been moved to a different location, thus the 3xx responses.
URL 重写可以纯粹在服务器端转换 URL。这使得 Web 应用程序开发人员能够从多个 URL 访问 Web 资源。
例如,用户可能请求
http://www.example.com/product/123
但由于重写,实际上提供了来自http://www.example.com/ 的资源产品?id=123
。请注意,浏览器中显示的地址无需更改。如果需要,可以更改地址。为此,服务器上会发生与上述类似的映射,但服务器不会将资源渲染回客户端,而是将重定向(301 或 302 HTTP 代码)发送回客户端以获取重写的 URL。
对于上面的示例,这可能如下所示:
客户端请求
服务器响应
此时,浏览器将对
Location
标头中的 URL 发出新的GET
请求。URL rewriting can transform URLs purely on the server-side. This allows web application developers the ability to make web resources accessible from multiple URLs.
For example, the user might request
http://www.example.com/product/123
but thanks to rewriting is actually served a resource fromhttp://www.example.com/product?id=123
. Note that, there is no need for the address displayed in the browser to change.The address can be changed if so desired. For this, a similar mapping as above happens on the server, but rather than render the resource back to the client, the server sends a redirect (301 or 302 HTTP code) back to the client for the rewritten URL.
For the example above this might look like:
Client request
Server response
At this point, the browser will issue a new
GET
request for the URL in theLocation
header.