the difference between redirection and rewrite

If you wrote rewrite rules, you may get confused by the rewrite and redirection.

What happens if you write the following RewriteRules?

If you type http://domainhostseotool.com/test1.html , the browser will display the content of test3.html while the url in the address bar is still http://domainhostseotool.com/test1.html. There is only one GET request (for test1.html) and one 200OK response (for the content of test3.html) between the client and the server. The client is never aware of  the existence of test2.html.

If you write the following RewriteRules:

The result seems the same: you will see the content of test3.html in the browser’s window. However, if you are careful enough, you will notice the url in the address bar has been changed from http://domainhostseotool.com/test1.html  to http://domainhostseotool.com/test3.html . The request/response between the client and the server also become more complex. First, a GET request is issued for test1.html, then a 302 redirection response is sent back with a Location header set to http://domainhostseotool.com/test3.html. Then the browser automatically issues the second request for test3.html and gets a 200OK response for the content of test3.html from the server. And the url is changed to http://domainhostseotool.com/test3.html because  the browser always shows the latest GET url in the address bar. The client also has no concept of test2.html.

Now consider the following RewriteRules:

What do you think about the http packets between the server and the client? You may imagine this scenario: a GET test1.html is sent and a 302 response is returned with a Location header set to test2.html, then a GET test2.html is sent and a 200OK response is returned with the content of test3.html, and the url in the address bar is changed to test3.html. Unfortunately, it is not correct. In fact, only one GET test1.html is sent and only one 302 response is returned with the content of test3.html. This 302 response has no Location header, thus the browser will not issue a second GET request. The point is if the final url(after parsing all rewrite rules and going through all iterations) is not in a [R] rule, Apache won’t use the Location header to instruct the browser to go to the second run.

 

Posted in tips of hosting