apache环境.htaccess设置301跳转及常用.htaccess技巧整理

无论是Nginx,还是Apache都各自有优势,对于我们普通用户而言基本上也没有多大的区别。在虚拟主机环境中,基本上都是Apache环境。Apache与Nginx两者区别之一就是伪静态的设置,前者则是在网站根目录设置.htaccess文件,无论是伪静态,还是301跳转,甚至是防盗链和禁止某个IP的访问都可以很简单的设置。

Apache环境.htaccess设置301跳转及常用.htaccess技巧整理

关于.htaccess文件的设置和实际应用是相当广泛的,很多功能都可以利用这个小小的文件实现,只不过我们普通用户使用的并不多而已,比如我们使用最多的无非就是WWW或者非WWW 301跳转,顶多加上防盗链等功能,在这篇文章中就是.htaccess文件的常见的用法。

第一、强制www域名301跳转

RewriteEngine on
RewriteCond %{HTTP_HOST} ^chinasoft\.com [NC]
RewriteRule ^(.*)$ https://www.chinasoft.com/$1 [L,R=301,NC]

我们将域名替换成自己的域名即可实现非WWW跳转到WWW域名。

第二、强制非WWW域名301跳转

RewriteEngine On
RewriteCond %{HTTP_HOST} !^chinasoft.com$ [NC]
RewriteRule ^(.*)$ http://chinasoft.com/$1 [L,R=301]

这里我们可以强制如果访问www域名前缀,会跳转到非www域名。

第三、强制https跳转地址

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.chinasoft.com/$1 [R,L]

同时满足强制跳转https和www的示例

# 强制跳转https和www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^chinasoft.io
RewriteRule ^(.*)$ https://www.chinasoft.io/$1 [R,L]

如何我们网站采用SSL证书,则一般需要强制跳转https路径,这里需要添加强制80端口的跳转。

第四、阻止无用的蜘蛛爬取

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (^$|FeedDemon|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms) [NC]
RewriteRule ^(.*)$ - [F]

第五、强制/斜杠结尾路径

RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

第六、取消/斜杠结尾

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

第七、将A页面301跳转到B页面

Redirect 301 /a.html https://www.chinasoft.com/b.html

第八、阻止某个IP访问

Order deny,allow
Allow from all
Deny from xxx.xxx.xxx.xxx

第九、禁止图片盗链

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(.+\.)?chinasoft.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|bmp)$ - [NC,F,L]

第十、禁止被其他网页套用

SetEnvIf Request_URI "/starry-night" allow_framing=true
Header set X-Frame-Options SAMEORIGIN env=!allow_framing

第十一、全部重定向到B网站

RewriteEngine on
RewriteRule ^(.*)$ http://www.chinasoft.com/$1 [R=301,L]

如果我们更换域名或者希望域名流量全部导入新网站,这里可以全部跳转。

第十二、去掉默认首页 index.html

# 去掉index.html
RewriteCond %{REQUEST_URI} ^(/index\.html)$ [NC]
RewriteRule ^(.*) / [R=301,L]

RewriteCond %{REQUEST_URI} !^(/index\.html)$ [NC]
RewriteCond %{REQUEST_URI} (/index\.html)$ [NC]
RewriteRule (.+)/index\.html /$1/ [R=301,L]

第十三、常用状态码全局跳转

ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 405 /405.html
ErrorDocument 500 /500.html
ErrorDocument 503 /503.html

ErrorDocument 这里填 403,404等错误信息调整页面,用来访问出现404页面等情况时的错误页面展示

总结,平时常用和不常用的.htaccess伪静态、301、防盗链以及等设置功能。如果需要更多强大的htaccess功能则需要阅读官方文档。

完整站点示例:

root@server1 [/home/chinasoft0807/public_html]# more .htaccess
# 打开zip 压缩
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/php
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/opentype
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^home\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /online/home.html [L]
</IfModule>

DirectoryIndex index.php index.html home.html
<Files 403.shtml>
order allow,deny
allow from all
</Files>

deny from 1.180.17.231

RewriteOptions inherit

<IfModule include_module>
   Options +IncludesNoExec
</IfModule>

AddType text/html .shtml  .html  .htm
AddOutputFilter INCLUDES .shtml  .html  .htm

RewriteEngine on
# 强制跳转https和www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^chinasoft.io
RewriteRule ^(.*)$ https://www.chinasoft.io/$1 [R,L]

# 去掉index.html
RewriteCond %{REQUEST_URI} ^(/index\.html)$ [NC]
RewriteRule ^(.*) / [R=301,L]

RewriteCond %{REQUEST_URI} !^(/index\.html)$ [NC]
RewriteCond %{REQUEST_URI} (/index\.html)$ [NC]
RewriteRule (.+)/index\.html /$1/ [R=301,L]

# /files 临时跳转 mm.edrasoft.com
Redirect 301 /files https://mm.chinasoft.com
# 404 调整默认首页
ErrorDocument 404 https://chinasoft.io/online/home.html

apache环境.htaccess设置301跳转及常用.htaccess技巧整理的更多相关文章

  1. .htaccess设置301跳转及常用技巧整理

    在虚拟主机环境中,基本上都是Apache环境.Apache的伪静态的设置,都是在网站根目录设置.htaccess文件,在.htaccess文件中无论是伪静态, 还是301跳转,甚至是防盗链和禁止某个I ...

  2. cdnbest设置301跳转

     cdnbest设置301跳转  格式:^http://kangleweb.com/(.*)$   https://www.kangleweb.com/$1  下面是站点里所有域名都跳转到https ...

  3. Apache环境.htaccess伪静态301跳转(www与不带www)

    一般而言,我们使用的301跳转就是WWW与不带WWW域名之间的跳转,用行话说就是权重要归一.对于我们使用Apache环境的来说其实相对nginx比较简单,因为只需要我们在网站根目录有.htaccess ...

  4. Apache环境下强制http跳转至https的配置总结

    一. 简单实例介绍一般来说,apache配置好http和https后,如果想要做http强转到https,需要设置url重定向规则,大致需要下面几个步骤即可完成配置: 1)在httpd.conf文件里 ...

  5. phpcms9添加301跳转

    在做301跳转时遇到了"错误 310 (net::ERR_TOO_MANY_REDIRECTS):重定向过多."的问题,小编在这里把处理方法简单给大家写出来希望可以帮助到大家,另外 ...

  6. Nginx/Apache之伪静态设置 - 运维小结

    一.什么是伪静态伪静态即是网站本身是动态网页如.php..asp..aspx等格式动态网页有时这类动态网页还跟"?"加参数来读取数据库内不同资料,伪静态就是做url重写操作(即re ...

  7. nginx 301跳转https后post请求失效问题解决

    app本地请求是http端口,后来升级https强制301跳转,设置如下 server { listen 80; server name www.XXX.com; rewrite ^/(.*)$ ht ...

  8. 如何在windows下Apache环境开启htaccess伪静态功能

     以下文章来自于网络,只做学习用 很多国人习惯用windows服务器或者在windows系统下调试PHP程序,在调试货使用的时候就遇到开启伪静态的各种问题,今天在网络上搜集了一些开启伪静态需要注意 ...

  9. apache用户认证、默认主机、301跳转

    我更正论坛一个同学帖子(今天坑我一下午):原文http://www.apelearn.com/bbs/foru ... 3%BB%A7%C8%CF%D6%A4 apache用户认证.默认主机.301跳 ...

随机推荐

  1. 关于mybaits的注解@Param

    一.含义 @param作为dao层的注解,为了解决多个参数时,参数类型不一致的问题. <select id="findEmpById" resultType="co ...

  2. Prometheus(五):Prometheus+Alertmanager 配置企业微信报警

    此处默认已安装Prometheus服务,服务地址:192.168.56.200  一.设置企业微信 1.1.企业微信注册(已有企业微信账号请跳过) 企业微信注册地址:https://work.weix ...

  3. Python与设计模式--工厂模式

    快餐点餐系统 想必大家一定见过类似于麦当劳自助点餐台一类的点餐系统吧.在一个大的触摸显示屏上,有3类可以选择的上餐品:汉堡等主餐.小食.饮料.当我们选择好自己需要的食物,支付完成后,订单就生成了.下面 ...

  4. JQuery系列(7) - JQuery最佳实践

    上篇文章是一篇入门教程,从设计思想的角度,讲解"怎么使用jQuery".今天的文章则是更进一步,讲解"如何用好jQuery". 我主要参考了Addy Osman ...

  5. IMP self _cmd

    The only way to circumvent dynamic binding is to get the address of a method and call it directly as ...

  6. python-随机生成验证码实例

    需求:随机生成验证码, 思路: 1.生成一个随机数,65-90 2.数字转化为字母:chr(数字) #!/usr/bin/env python # -*- coding:utf-8 -*- impor ...

  7. Word Squares

    Description Given a set of words without duplicates, find all word squares you can build from them. ...

  8. RFM - Customer Level Data

    Introduction ## Warning: package 'DT' was built under R version 3.5.2 RFM (recency, frequency, monet ...

  9. eclipse中的maven插件

    导入一个maven项目,一直报错:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的错误 Description ...

  10. UmiJS可插拔的企业级 react 应用框架,配合ant-design-pro使用

    入门非常简单 # 安装 $ yarn global add umi # 或者 npm install -g umi # 新建应用 $ mkdir myapp && cd myapp # ...