Apache 域名301跳转
比如网站可以通过tu17.com访问,也可以通过www.tingcd.net访问。所以想实现tingcd.net全部重定向(跳转)到www.tingcd.net。同时按照google的建议,使用服务器端 301 重定向,为了确保用户及搜索引擎定向至正确网页的最佳方法。301 状态代码表示某网页已被永久迁移至新位置。下面将了解一下apache下实现301永久性重定向2个方法,需要具有访问服务器的 .htaccess 文件的权限。
1. Apache模块 mod_alias的 Redirect 和 RedirectMatch命令
上面提到2个命令使用方法相似。而区别就是后者RedirectMatch基于正则表达式匹配对当前的URL发送一个外部重定向语法为:
Redirect [status] URL-path URLRedirectMatch [status] regex URL
status参数可以使用以下HTTP状态码:
permanent返回一个永久性重定向状态码(301),表示此资源的位置变动是永久性的。temp返回一个临时性重定向状态码(302),这是默认值。seeother返回一个"参见"状态码(303),表示此资源已经被替代。gone返回一个"已废弃"状态码(410),表示此资源已经被永久性地删除了。如果指定了这个状态码,则URL参数将被忽略。举例:
APACHE
- Redirect 301 /old/old.htm http://www.tingcd.net/new.htm
 - Redirect permanent /one http://tingcd.net/two
 - RedirectMatch 301 (.*)\.gif$ http://www.tingcd.net/images/$1.jpg
 
2.使用mod_rewrite重写URL方式
APACHE
- Options +FollowSymLinks
 - RewriteEngine on
 - RewriteCond %{HTTP_HOST} ^tingcd\.net
 - RewriteRule ^(.*)$ http://www.tingcd.net/$1 [R=permanent,L]
 
在这里判断当前服务器变量HTTP_HOST是否等于tingcd.net,为真就进行重写,按照R=permanent进行永久重定向,L表示并立即停止重写操作,并不再应用其他重写规则
下面是我最终实现的.htaccess文件,同时也并入wordpress重写规则。
APACHE
- <IfModule mod_rewrite.c>
 - RewriteEngine On
 - #Redirect
 - Options +FollowSymLinks
 - RewriteCond %{HTTP_HOST} ^zivee\.cn$
 - RewriteCond %{HTTP_HOST} !^$
 - RewriteRule ^(.*)$ http://www.tingcd.net/$1 [R=301,L]
 - </IfModule>
 
相关推荐
  服务器端攻城师    2020-05-04  
   etedyh    2020-01-02  
   咻咻ing    2010-11-09  
   lsn0LSN    2014-10-23  
   zhangpuego    2013-03-18  
   greatking    2013-01-05  
   单调的低调    2015-04-09  
   奶牛老爹    2011-05-04  
   qiuqiang    2016-04-07  
   jingz    2011-09-09  
   bocaimomo    2013-08-01  
   leeagle    2012-12-23  
   ibatsiSpring    2012-08-11  
   xiajlxiajl    2017-03-08  
   清风落叶    2016-08-08  
   lihangorz    2018-08-09  
   leehbing    2019-04-20  
   liuchen0    2017-06-06  
   phptyong    2019-04-15  
 