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?

- 1,558
- 2
- 12
- 24
-
Take a look in RFC 2616, section 10.3: http://www.ietf.org/rfc/rfc2616.txt – Anders Lindahl Jun 21 '11 at 19:28
5 Answers
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:
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).
-
can you please explain `RewriteCond %{REDIRECT_URL} !^$ RewriteRule .* - [L]` a bit more – Vipin Verma Nov 06 '13 at 14:15
-
Apache creates this redirect variable when an internal redirection occurs. So you can check if this is empty before processing a new redirection. – M'vy Nov 06 '13 at 16:00
-
The rewrite flags are explained here [link](http://httpd.apache.org/docs/2.2/rewrite/flags.html) – Athiruban May 31 '14 at 19:30
-
Something that is often unclear: - HTTP redirects are part of a standard. The browser will interpret an HTTP redirect response as "hey browser, please load this URL instead". - URL rewrites are a server feature. The server get a request on "/foo/bar" but prefer to return the content of "/hello-world". Example use case: url is rewritten to "/foobar/a" or "/foobar/b" depending on a cookie in order to implement an A/B test. A redirect would not be correct, as the end user must not be aware of their A/B test bucket "a" or "b". – Eric Burel Jul 05 '22 at 15:38
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.

- 164
- 4
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.

- 39,672
- 31
- 167
- 226
-
-
1@Davy8, I was in the middle of editing my question and adding all that information when my boss called me into his office :)... it's too late now. – Kiril Jun 21 '11 at 19:59
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 tohttp://servername/foo/dirname/
.

- 119,590
- 31
- 270
- 376
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.

- 3,445
- 26
- 26