Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does url rewrite works?

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?

like image 723
cirne100 Avatar asked Jun 21 '11 19:06

cirne100


2 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:

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).

like image 183
M'vy Avatar answered Oct 04 '22 17:10

M'vy


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.

like image 30
tbombach Avatar answered Oct 04 '22 16:10

tbombach