網站加速最佳實踐 – 避免重定向
參考: http://developer.yahoo.com/performance/rules.html#redirects
Redirects are accomplished using the 301 and 302 status codes. Here's an example of the HTTP headers in a 301 response:
HTTP/1.1 301 Moved Permanently
Location: http://example.com/newuri
Content-Type: text/html
The browser automatically takes the user to the URL
specified in the Location
field. All the information necessary for
a redirect is in the headers. The body of the response is typically empty.
Despite their names, neither a 301 nor a 302 response is cached in practice
unless additional headers, such as Expires
or Cache-Control
,
indicate it should be. The
meta refresh tag and JavaScript are other ways to direct users to a different
URL, but if you must do a redirect, the preferred technique is to use the
standard 3xx HTTP status codes, primarily to ensure the back button works
correctly.
The main thing to remember is that redirects slow down the user experience. Inserting a redirect between the user and the HTML document delays everything in the page since nothing in the page can be rendered and no components can start being downloaded until the HTML document has arrived.
如果少了網址最後的“/”會讓重定向更慢!
One of the most wasteful redirects happens frequently and
web developers are generally not aware of it. It occurs when a trailing slash
(/) is missing from a URL that should otherwise have one. For example, going to
http://astrology.yahoo.com/astrology
results in a 301 response containing a redirect to http://astrology.yahoo.com/astrology/
(notice the added trailing slash). This is fixed in Apache by using Alias
or mod_rewrite
,
or the DirectorySlash
directive if you're using Apache handlers.
從舊網站連結到新網站通常會使用重定向,這樣做雖然簡單,但是會降低使用者體驗(速度)。
Connecting an old web site to a new one is another common
use for redirects. Others include connecting different parts of a website and
directing the user based on certain conditions (type of browser, type of user
account, etc.). Using a redirect to connect two web sites is simple and
requires little additional coding. Although using redirects in these situations
reduces the complexity for developers, it degrades the user experience.
Alternatives for this use of redirects include using Alias
and mod_rewrite
if the two code paths are hosted on the same server. If a domain name change is the cause of using
redirects, an alternative is to create a CNAME (a DNS record that creates an
alias pointing from one domain name to another) in combination with Alias
or mod_rewrite
.
301 redirect: 301 代表永久性轉移 (Permanently Moved) ,
302 redirect: 302 代表暫時性轉移 (Temporarily Moved ) ,
參考: http://blog.joycode.com/ghj/archive/2007/09/21/108696.aspx
相關文章
- 避免故障逃逸最佳實踐
- 全民加速節:全站加速在網際網路媒體應用上的最佳實踐
- 怎樣修改公司網站內容,公司網站內容更新最佳實踐網站
- 全民加速節:全站加速在遊戲行業的最佳實踐遊戲行業
- HarmonyOS Next 網路加速進階:最佳化策略與應用實踐
- 容器映象加速指南:探索 Kubernetes 快取最佳實踐快取
- 最佳實踐:使用阿里雲CDN加速OSS訪問阿里
- 網站人工最佳化實戰:專業、高效的網站最佳化!網站
- 阿里雲全站加速在遊戲行業的最佳實踐阿里遊戲行業
- 專案範圍管理的最佳實踐:避免軟體專案膨脹
- css網格佈局的最佳實踐CSS
- 冬季實戰營 動手實戰-最佳應用實踐,使用PolarDB和ECS搭建入口網站網站
- Serverless實踐-靜態網站託管Server網站
- AutoMapper 最佳實踐APP
- 《.NET最佳實踐》
- Django 最佳實踐Django
- metaq最佳實踐
- springDataJpa 最佳實踐Spring
- KeyPath 最佳實踐
- Pika最佳實踐
- JavaScript 最佳實踐JavaScript
- SnapKit 最佳實踐APK
- JDBC 最佳實踐JDBC
- Kafka最佳實踐Kafka
- Iptables 最佳實踐 !
- Serilog 最佳實踐
- Flutter 最佳實踐Flutter
- Java最佳實踐Java
- MongoDB 最佳實踐MongoDB
- Gradle最佳實踐Gradle
- 跨境網際網路券商架構最佳實踐\n架構
- 安裝SSL證書的網站如何實現HTTP重定向到HTTPS網站HTTP
- 節省數億IT成本,B站FinOps最佳化實踐
- 網站加速的【5大因素】網站
- 分享如何避免您的網站被入侵?網站
- 網站如何避免谷歌SEO的懲罰?網站谷歌
- 網易蜂巢:基於容器和微服務迭代加速實踐微服務
- TensorFlow搭建神經網路最佳實踐樣例神經網路
- 京東短網址高可用提升最佳實踐