Apache环境.htaccess常用跳转规则

2018年2月15日09:41:36来源:深冬博客 评论 787

最近出于建站需要设置301重定向,现在记录一下.htaccess文件常用301跳转规则、https跳转规则,以备以后查看。

301、302重定向

旧域名跳转到新域名

  1. 访问指定域名跳转
  2. RewriteEngine On
  3. RewriteCond %{HTTP_HOST} ^example1.com$ [NC]
  4. RewriteRule ^(.*)$ http://example2.com/$1 [L,R=301]
  5. 全站跳转
  6. RewriteEngine On
  7. RewriteRule ^(.*)$ http://example2.com/$1 [L,R=301]

www跳转到裸域

  1. RewriteEngine On
  2. RewriteCond %{HTTP_HOST} !^example.com$ [NC]
  3. RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
  4. RewriteEngine On
  5. RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
  6. RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

裸域跳转到www

  1. RewriteEngine On
  2. RewriteCond %{HTTP_HOST} ^example.com$ [NC]
  3. RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  4. RewriteEngine On
  5. RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
  6. RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

https强制跳转

  1. RewriteEngine On
  2. RewriteCond %{SERVER_PORT} ^80$
  3. RewriteRule ^(.*)$ https://xxx.com/$1 [R=301,L]
  4. RewriteEngine On
  5. RewriteCond %{SERVER_PORT} !^443$
  6. RewriteRule ^(.*)$ https://xxx.com/$1 [R=301,L]

排除某个目录

  1. RewriteCond $1 !^(DATA|images|otherThings)
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: