登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

SeaRiver Blog

实力才是你一生最好的依靠!

 
 
 

日志

 
 

Apache Web Server Configuration for Web Site Redirection  

2009-04-23 13:55:07|  分类: apache |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
List of methods used to redirect a web site using Apache:

Web site forwarding and redirection methods:

  1. One can forward a web page URL or home page using the following web page with the "Refresh" directive:
    <META HTTP-EQUIV="Refresh" Content="0; URL=http://www.company.com/dir1/">
    This commands the browser to refresh the page with the new specified URL. This forwards a single page only and not the entire domain. It can forward the default home page for the domain giving the appearance of forwarding the domain..

    or:

    <html>
    <head>
    <META HTTP-EQUIV="Refresh" Content="3; URL=http://www.company.com/dir1/">
    </head>
    <body>
    This page will forward to http://www.company.com/dir1/ in three seconds.
    <p>
    Please update your links.
    </body>
    </html>

  2. Use a CGI script to forward a home page: (mod_cgi)
    File: httpd.conf
    ScriptAlias / /var/www/cgi-bin/redirect-script/
    File: /var/www/cgi-bin/redirect-script
    #!/usr/bin/perl    print "Status: 301 Moved\r\n" .        "Location: http://www.new-domain.com/\r\n" .        "\r\n";                      
    or:
    #!/usr/bin/perl -w  use strict;  use CGI qw/:standard/;  print redirect('http://www.new-domain.com');                      

  3. Use a PHP script to redirect:
    <?php  header("Location: http://www.new-domain.com/");  ?>                      
  4. Use a Javascript to redirect:
    <html>  <head>  <script language="Javascript" type="text/javascript">  <!-- Hide script  //<![CDATA[  window.location.href="http://www.new-domain.com/"       //]]> End script hiding -->  </script>  </head>  </html>                      
  5. Use Apache module (mod_rewrite)
    File: httpd.conf
    RewriteEngine On
    RewriteRule /.* http://www.new-domain.com/ [R]
    Forwards all references in entire domain.

  6. Use Apache module (mod_alias )
    File: httpd.conf
    • Redirect Domain:
      Redirect / http://www.new-domain.com/
      or
      Redirect permanent / http://www.new-domain.com/
    • Redirect Page:
      Redirect /web-page.html http://www.new-domain.com/destination-web-page.html                          
    Note:
    • Redirect directives take precedence over Alias and ScriptAlias directives.
    • Other "Redirect" options include: temp (error 302) default - temporary redirect status, seeother (error 303) resource has been replaced and gone (error 410) resource has been permanently removed.

    Example httpd.conf with virtual hosts for multiple domains which all redirect:

    <VirtualHost XXX.XXX.XXX.XXX>  ServerName directtolinux.com  ServerAlias www.directtolinux.com  ServerAlias direct-to-linux.com  ServerAlias www.direct-to-linux.com  ServerAlias digitalpenguins.com  ServerAlias www.digitalpenguins.com  Redirect permanent / http://www.yolinux.com/  </VirtualHost>                          

  7. Apache 301 redirect using the .htaccess file:

    If one wants to permanently forward an entire web site to a new URL or forward a single page permanently and have the search engines update their database, one should use a 301 redirect. This may redirect to a new server or to itself but to a different domain. This tutorial shows how. This method is a variation of using the mod_alias redirection shown above except that it allows the customer to redirect themselves by providing a .htaccess file themselves.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^yolinux.com
    RewriteRule ^(.*)$ http://www.yolinux.com/$1 [R=permanent,L]
    This example forwards http://yolinux.com to http://www.yolinux.com/ to unify your site to a single URL. This can also simplify your web logs if they can not distinguish between the two.


(Method 5) Apache configuration for redirect using httpd.conf and .htaccess:

This configures Apache to command the web browser to redirect by performing a GET from the "redirected" web site the user is being forwarded to.

File: /etc/httpd/conf/httpd.conf (older systems used access.conf)

Default: This disables the processing of .htaccess files for the system.

         <Directory />
AllowOverride None
</Directory>

or for a specified directory:

         <Directory /home/domain/public_html>
AllowOverride None
</Directory>

Specify directory containing site or page to be redirected:

         <Directory /root-directory-of-web-site-to-be-redirected>
AllowOverride All
</Directory>

AllowOverride parameters: AuthConfig FileInfo Indexes Limits Options


File: .htaccess Create a file /home/domain/public_html/.htaccess in that directory of the domain to be forwarded that looks something like this:
  • Redirect entire domain:
    Redirect 301 /  http://www.new-domain.com/
    Note: The use of the "/" at the end of the redirected domain. This is necessary so that http://www.old-domain.com/page1.html will be redirected to http://www.new-domain.com/page1.html.
    OR
  • Redirect specified pages:
    Redirect 301 /old-page-1.html  http://www.newdomain.com/new-page-1.html
    Redirect 301 /old-page-2.html http://www.newdomain.com/new-page-2.html

You may use the following directives:

  • 301: permanent
  • 302: temp
  • 303: seeother
  • 410: gone
For example:
Redirect permanent /  http://www.newdomain.com/

If an incorrect directive is used in the httpd.conf or .htaccess file it will result in a server error. Check your log files: /var/log/httpd/error_log.


HTTP 1.1 Redirect codes:

HTTP Code Status Description
301 permanent The resource has permanently moved
302 temp The resource has temporarily moved
303 seeother The resource has been replaced and refer to new resource
305 UseProxy Use proxy to access site
307 Temp The resource has temporarily moved
410 Tegone The resource has permanently removed

See RFC 2616 HTTP/1.1 protocol - Chapter 10.3


Links:

原地址:http://www.yolinux.com/TUTORIALS/ApacheRedirect.html

  评论这张
 
阅读(1250)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018