[ACCEPTED]-How does url rewrite works?-httpwebrequest

Accepted answer
Score: 35

There are two types of behaviour.

One is 18 rewrite, the other is redirect.

Rewrite

The server 17 performs the substitution for itself, making 16 a URL like http://example.org/my/beatuful/page be understood as http://example.org/index.php?page=my-beautiful-page

With rewrite, the 15 client does not see anything and redirection 14 is internal only. No URL changes in the 13 browser, just the server understands it 12 differently.

Redirect

The server detects that the 11 address is not wanted by the server. http://example.org/page1 has 10 moved to http://example.org/page2, so it tells the browser with 9 an HTTP 3xx code what the new page is. The 8 client then asks for this page instead. Therefore 7 the address in the browser changes!

Process

The process 6 remains the same and is well described by 5 this diagram:

enter image description here

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

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

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

Score: 6

Are you talking about server-side rewrites 7 (like Apache mod-rewrite)? For those, the 6 address bar does not generally change (unless 5 a redirection is performed). Or are you 4 talking about redirections? These are done 3 by having the server respond with an HTTP 2 code (301, 302 or 307) and the location 1 in the HTTP header.

Score: 2

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

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

URL 15 rewriting and forwarding are two completely 14 different things. A server has no control over your browser so it can't change the 13 URL of your browser, but it can ask your 12 browser to go to a different URL. When your 11 browser gets a response from a server it's 10 entirely up to your browser to determine 9 what to do with that response: it can follow 8 the redirect, ignore it or be really mean 7 and spam the server until the server gives 6 up. There is no "mechanism" that 5 the server uses to change the address, it's 4 simply a protocol (HTTP 1.1) that the server 3 abides by when a particular resource has 2 been moved to a different location, thus 1 the 3xx responses.

Score: 2

There are two forms of "URL rewrite": those 22 done purely within the server and those 21 that are redirections.

If it's purely within 20 the server, it's an internal matter and 19 only matters with respect to the dispatch 18 mechanism implemented in the server. In 17 Apache HTTPD, mod_rewrite can do this, for example.

If 16 it's a redirection, a status code implying 15 a redirection is sent in the response, along 14 with a Location header indicating to which URL the 13 browser should be redirected (this should 12 be an absolute URL). mod_rewrite can also do this, with 11 the [R] flag. The status code is usually 302 (found), but 10 it could be configured for other codes (e.g. 301 9 or 307).

Another quite common use (often 8 unnoticed because it's usually on by default 7 in Apache HTTPD) is the redirection to the 6 the URL with a trailing slash on a directory. This 5 is implemented by mod_dir:

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

Score: 1

URL rewriting can transform URLs purely 17 on the server-side. This allows web application 16 developers the ability to make web resources 15 accessible from multiple URLs.

For example, the 14 user might request http://www.example.com/product/123 but thanks to rewriting 13 is actually served a resource from http://www.example.com/product?id=123. Note 12 that, there is no need for the address displayed 11 in the browser to change.

The address can 10 be changed if so desired. For this, a similar 9 mapping as above happens on the server, but 8 rather than render the resource back to 7 the client, the server sends a redirect 6 (301 or 302 HTTP code) back to the client 5 for the rewritten URL.

For the example above 4 this might look like:

Client request

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

Server 3 response

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

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

More Related questions