Rule 11: Avoid Redirects
Types of Redirects
- 300: Multiple Choices(based on Content-Type)
- 301: Moved Permanently
- 302: Moved Temporarily
- 303: See Other(clarification of 302)
- 304: Not Modified .. It is not really a redirect -- it's used in response to conditional GET requests to avoid downloading data that is already cached by the browser.
- 305: Use Proxy
- 306: (no longer used)
- 307: Temporary Redirect (clarification of 302)
The 301 and 302 status codes are the ones used most often. Status codes 303 and 307 were added in the HTTP/1.1 specification to clarigy the (mis)use of 302, but the adoption of 303 and 307 is almost nonexistent, as most web sites continue to use 302.
另外一种自动跳转的方法是, 使用meta的refresh, 如
<meta http-equiv="refresh" content="0; url=http://stevesounders.com/newuri">
Alternatives to Redirects
- a trailing slash(/) is missing from a URL that should otherwise have one. autoindexing使得当请求的url不存在时去寻找些默认的页面. 注意如果主机名后来没加/是不会redirect.
- use the mod_rewrite,
Alias /astrology /usr/local/apache/htdocs/astrology/index.html
<Location /astrology> DirectorySlash Off SetHandler astrologyhandler </Location>
Connecting Web Sites
- Alias, mod_rewrite, and DirectorySlash require committing to an interface (handlers or filenames) in addition to URLs, but are simple to implement.
- If the rwo backends reside on the same server, it's likely that code itself could be linked. For example, the older handler code could call the new handler code programmatically.
- If the domain name changes, a CNAME (a DNS record that creates an alias pointing from one domain name to another) can be used to make both hostnames point to the same servers. If this is possible, the techniques mentioned here(Alias, mod_rewrite, DirectorySlash, and directly linking code) are viable.
The key is to find a way to have these simpler URLs without the redirects. Rather than forcing users to undergo an additional HTTP request, it would be better to avoid the redirect using Alias, mod_rewrite, DirectorySlash, and directly linking code.
Find ways to avoid redirects.