How does url rewrite works?

2019-01-13 22:22发布

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?

5条回答
淡お忘
2楼-- · 2019-01-13 22:42

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 as http://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 to http://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:

enter image description here

Remark Every rewrite/redirect triggers a new call to the rewrite rules (with exceptions IIRC)

RewriteCond %{REDIRECT_URL} !^$
RewriteRule .* - [L]

can become useful to stop loops. (Since it makes no rewrite when it has happened once already).

查看更多
可以哭但决不认输i
3楼-- · 2019-01-13 22:45

Jeff Atwood had a great post about this: http://www.codinghorror.com/blog/2007/02/url-rewriting-to-prevent-duplicate-urls.html

How web server implements url rewrite mechanism and changes the address bar of browsers?

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.

查看更多
▲ chillily
4楼-- · 2019-01-13 22:55

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:

A "trailing slash" redirect is issued when the server receives a request for a URL http://servername/foo/dirname where dirname is a directory. Directories require a trailing slash, so mod_dir issues a redirect to http://servername/foo/dirname/.

查看更多
疯言疯语
5楼-- · 2019-01-13 22:59

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 from http://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

GET /product/123 HTTP/1.1
Host: www.example.com

Server response

HTTP/1.1 302 Found
Location: http://www.example.com/product?id=123

At this point, the browser will issue a new GET request for the URL in the Location header.

查看更多
唯我独甜
6楼-- · 2019-01-13 23:08

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.

查看更多
登录 后发表回答