HTML是一种用于构建网页的语言。网页具有动态性,它可以随时间变化。最流行的更改案例之一是将给定的网页重定向到另一个网页。这就是所谓的网页重定向。在本教程中,我们将以不同的方式、语言和技术检查重定向过程。
HTML重定向
最流行和最基本的网页重定向是使用HTML。HTML有两个主要部分,分别是
和
. 我们可以提供一些特殊的标签
以重定向HTML页。我们将使用
使用详细属性标记。在本例中,我们将重定向到 https://www.poftut.com
. 我们将使用 http-equiv
属性 refresh
价值观。
JavaScript重定向
JavaScript是一种客户端技术,可以在HTML页面加载之后或加载期间进行动态更改。JavaScript语言提供 window.location
对象,用于获取和设置当前页URL。
windows.location="https://www.poftut.com";
或者JavaScript提供了不同的机制来更改或重定向HTML网页。
windows.location="https://www.poftut.com";windows.location.href="https://www.poftut.com";windows.location.assign("https://www.poftut.com");windows.location.replace("https://www.poftut.com");
Apache重定向
Apache是一种流行的web服务器。我们可以在服务器端使用Apache重定向页面。我们可以用 Redirect
或 RedirectMatch
完全或特定地重定向网页的指令。
我们可以从 /blog
.
RedirectMatch 301 /blog(.*) https://www.poftut.com$1
或者我们可以重定向特定的网页,如 page.html
在下面的示例中。
Redirect 301 /page.html https://www.poftut.com/new-page.html
Nginx重定向
Nginx是另一种流行的web服务器,用于服务web页面。它可以重定向网页使用 return
指令。这也可用于重定向 http
网页到 https
版本。
server { listen 80; server_name poftut.com; return 301 $scheme://poftut.com$request_uri;}
Lighttpd重定向
Lighttpd是一个web服务器,用于服务器lightwe站点。我们可以用 mode_redirect
模块及其应用 url.redirect
函数重定向HTML网页。在本例中,我们将重定向 http://www.poftut.com
进入之内 https://www.poftut.com
.
server.modules = ( "mod_redirect" ) $HTTP["host"] =~ "^(www.)?poftut.com$" { url.redirect = ( "^/(.*)$" => "https://www.poftut.com/$1", ) }
PHP重定向
PHP提供了HTML重定向特性 header()
功能。事实上 header()
函数将在HTTP响应中插入一个HTML元标记。 header()
我们只提供 Location:
使用我们要重定向的URL。
Ruby on Rails重定向
RubyonRails提供 ApplicationController
类可以继承到我们的类中。我们可以用 index
函数和put redirect_to
用于重定向的函数。
class WelcomeController < ApplicationController def index redirect_to 'http://poftut.com', :status => :moved_permanently end end
.Net重定向
.Net提供了C#和Visual Basic等语言。我们可以用 Reponse
类及其函数 Redirect()
和属性 Status
, AddHeader
.
Response.Redirect("http://www.poftut.com");
或者
Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", "http://www.poftut.com");
或者
Response.RedirectPermanent("http://www.poftut.com")
Node.js重定向
Node.js提供 writeHead()
函数 http
图书馆。我们可以重定向到 http
使用以下代码。
var http = require("http"); http.createServer(function(req, res) { res.writeHead(301,{Location: 'http://www.poftut.com'}); res.end(); }).listen(80);
烧瓶重定向
Flask是一个python框架,它使用 app.route()
并提供 redirect()
.
@app.route('/notes/') def thing(page): return redirect("http://www.poftut.com/blog/" + page, code=301)
Golang重定向
Golang提供 net/http
提供 http.HandleFunc()
处理HTTP响应并使用 Redirect()
函数来编写新的重定向URL。
package main import "net/http" func main() { http.HandleFunc("/", func (wr http.ResponseWriter, req *http.Request) { http.Redirect(wr, req, "http://poftut.com", http.StatusMovedPermanently) }) }
相关文章: 什么是超链接?