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. Plan B

    王兴曾经说过: 2019 年是过去 10 年中最差的一年,也是未来 10 年中最好的一年. 之前我希望王兴预判错了,但现在我发现这位掌控着生活消费类数据的大佬应该不是扯淡. 今年的内部和外部环境真的很 ...

  2. 【异常】ERROR in ch.qos.logback.core.joran.spi.Interpreter@159:22 - no applicable action for [charset], current ElementPath is [[configuration][appender][encoder][charset]]

    一.异常信息 Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at ...

  3. 学习Java书籍推荐和面试网站推荐

    一.Java书籍推荐: 来自http://www.importnew.com/26932.html 1. 鸟哥的Linux私房菜—基础学习篇 3. Effective Java 6. Java并发编程 ...

  4. es6 -- 与解构赋值默认值结合使用

    参数默认值可以与解构赋值的默认值,结合起来使用. function foo({x, y = 5}) { console.log(x, y) } foo({}) // undefined 5 foo({ ...

  5. python - django 设置自定义文件保存路径

    一. settings.py MEDIA_URL = "/qir/" # 设置获取文件时的访问根路径 MEDIA_ROOT = os.path.join(BASE_DIR, &qu ...

  6. Promise对异步编程的贡献以及基本API了解

    异步: 核心: 现在运行的部分和将来运行的部分之间的关系 常用方案: 从现在到将来的等待,通常使用一个回调函数在结果返回时得到结果 控制台(因为console族是由宿主环境即游览器实现的)可能会使用异 ...

  7. 洛谷/Codeforces CF865D 题解

    若想要深入学习反悔贪心,传送门. Description: 已知接下来 \(n\) 天的股票价格,每天可以买入当天的股票,卖出已有的股票,或者什么都不做,求 \(n\) 天之后最大的利润. Metho ...

  8. Spatiotemporal continuous estimates of PM2.5 concentrations in China, 2000–2016: A machine learning method with inputs from satellites, chemical transport model, and ground observations

    写在前面 首先,看完查了一下这个期刊 Environment International,是1区的文章,影响因子7.297!不愧是优秀的期刊,文章质量很高,内容很丰富. 内容 主要是PM25反演,利用 ...

  9. 第08组 Alpha冲刺(6/6)

    队名:955 组长博客:https://www.cnblogs.com/cclong/p/11913269.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...

  10. IDE 问题及解决

    目录 Eclipse 篇 1.MarketPlace 打不开,对话框闪退 2.使用 lombok ,预编译不通过 3.Eclipse + PyDev - > Unresolved import: ...