欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

301网页重定向代码大全

程序员文章站 2022-05-05 13:44:38
...
  1. Header( "HTTP/1.1 301 Moved Permanently" );
  2. Header( "Location: http://www.new-url.com" );
  3. ?>
复制代码

3. ASP Redirect

  1. Response.Status="301 Moved Permanently"
  2. Response.AddHeader "Location","http://www.new-url.com/"
  3. %>
复制代码

4. ASP .NET Redirect

复制代码

5. JSP Redirect

  1. response.setStatus(301);
  2. response.setHeader( "Location", "http://www.new-url.com/" );
  3. response.setHeader( "Connection", "close" );
  4. %>
复制代码

6. CGI PERL Redirect

  1. $q = new CGI;
  2. print $q->redirect("http://www.new-url.com/");
复制代码

7. Ruby on Rails Redirect

  1. def old_action
  2. headers["Status"] = "301 Moved Permanently"
  3. redirect_to "http://www.new-url.com/"
  4. end
复制代码

8. ColdFusion Redirect

  1. <.cfheader statuscode="301" statustext="Moved permanently">
  2. <.cfheader name="Location" value="http://www.new-url.com">
复制代码

9. Javascript URL Redirect

复制代码

10. IIS Redirect 在 Internet 服务管理器中右击你想要重定向的文件和文件夹,选择 "a redirection to a URL". (bbs.it-home.org 编辑整理) 然后输入目标网址,选中 "The exact url entered above" 和 "A permanent redirection for this resource" 然后点击 'Apply' 按钮。

11. 使用 .htaccess 进行重定向 创建一个 .htaccess 文件用来将访问 domain.com 重定向到 www.domain.com 下,该文件必须放置在网站的root目录,也就是首页放置的目录。

  1. Options +FollowSymlinks
  2. RewriteEngine on
  3. rewritecond %{http_host} ^domain.com [nc]
  4. rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
复制代码

注意: .htaccess 方法的重定向只能在 Linux 下使用 Apache 的 mod_rewrite 模块启用的情况下使用。

推荐阅读:
  • .htaccess实现301域名重定向
  • apache rewrite重定向的例子
  • htaccess 重定向所有请求到某个URL地址
  • htaccess防盗链、301重定向、限制网站访问
  • apache 301重定向配置实例
  • apache 301重定向

12.Nginx中的rewrite

  1. server {
  2. server_name bbs.it-home.org jbxue.com;
  3. if ($host = 'jbxue.com' ) {
  4. rewrite ^/(.*)$ http://bbs.it-home.org/$1 permanent;
  5. }
复制代码