apache环境.htaccess设置301跳转及常用.htaccess技巧整理
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技巧整理的更多相关文章
- .htaccess设置301跳转及常用技巧整理
在虚拟主机环境中,基本上都是Apache环境.Apache的伪静态的设置,都是在网站根目录设置.htaccess文件,在.htaccess文件中无论是伪静态, 还是301跳转,甚至是防盗链和禁止某个I ...
- cdnbest设置301跳转
cdnbest设置301跳转 格式:^http://kangleweb.com/(.*)$ https://www.kangleweb.com/$1 下面是站点里所有域名都跳转到https ...
- Apache环境.htaccess伪静态301跳转(www与不带www)
一般而言,我们使用的301跳转就是WWW与不带WWW域名之间的跳转,用行话说就是权重要归一.对于我们使用Apache环境的来说其实相对nginx比较简单,因为只需要我们在网站根目录有.htaccess ...
- Apache环境下强制http跳转至https的配置总结
一. 简单实例介绍一般来说,apache配置好http和https后,如果想要做http强转到https,需要设置url重定向规则,大致需要下面几个步骤即可完成配置: 1)在httpd.conf文件里 ...
- phpcms9添加301跳转
在做301跳转时遇到了"错误 310 (net::ERR_TOO_MANY_REDIRECTS):重定向过多."的问题,小编在这里把处理方法简单给大家写出来希望可以帮助到大家,另外 ...
- Nginx/Apache之伪静态设置 - 运维小结
一.什么是伪静态伪静态即是网站本身是动态网页如.php..asp..aspx等格式动态网页有时这类动态网页还跟"?"加参数来读取数据库内不同资料,伪静态就是做url重写操作(即re ...
- nginx 301跳转https后post请求失效问题解决
app本地请求是http端口,后来升级https强制301跳转,设置如下 server { listen 80; server name www.XXX.com; rewrite ^/(.*)$ ht ...
- 如何在windows下Apache环境开启htaccess伪静态功能
以下文章来自于网络,只做学习用 很多国人习惯用windows服务器或者在windows系统下调试PHP程序,在调试货使用的时候就遇到开启伪静态的各种问题,今天在网络上搜集了一些开启伪静态需要注意 ...
- apache用户认证、默认主机、301跳转
我更正论坛一个同学帖子(今天坑我一下午):原文http://www.apelearn.com/bbs/foru ... 3%BB%A7%C8%CF%D6%A4 apache用户认证.默认主机.301跳 ...
随机推荐
- 关于mybaits的注解@Param
一.含义 @param作为dao层的注解,为了解决多个参数时,参数类型不一致的问题. <select id="findEmpById" resultType="co ...
- Django 之瀑布流实现
需求分析 现在是 "图片为王"的时代,在浏览一些网站时,经常会看到类似于这种满屏都是图片.图片大小不一,却按空间排列,就这是瀑布流布局. 以瀑布流形式布局,从数据库中取出图片 每次 ...
- Linux 定时任务的控制台导出导入到文件
这个不但适用于定时任务,也适用于一般的脚本. 以前喜欢用>>这样的快捷方式, 今天看到用tee命令的,也记下. 55 23 * * * /bin/sh /sql_bak/restore_s ...
- django最小程序开发流程
1.建立工程 在工程目录下打开cmd,输入以下命令.其中mysite是项目名称. django-admin startproject mysite 命令运行完后,在该目录下会出现一个名为mysite的 ...
- 数据结构 - 顺序栈的实现 C++
顺序栈封装 C++ 使用C++对顺序栈进行了简单的封装,实现了栈的基本操作 封装方法: pop(),top(),size(),empty(),push() 代码已经过测试 #pragma once # ...
- CF472G Increase the Constraints
Increase the Constraints 定义两个等长的01字符串的汉明距离为它们字符不同的对应位置的个数. 给你两个01串S,T,现在有q个询问,每次指定S,T中两个定长的子串询问它们的汉明 ...
- spring相关—AOP编程—切入点、连接点
1 切入点表达式 1.1 作用 通过表达式的方式定位一个或多个具体的连接点. 1.2 语法细节 ①切入点表达式的语法格式 execution([权限修饰符] [返回值类型] [简单类名/全类名] [方 ...
- Linux套接字与虚拟文件系统(1):初始化和创建
http://www.cppblog.com/qinqing1984/archive/2015/05/03/210521.html 引言 在Unix的世界里,万物皆文件,通过虚拟文件系统VFS,程 ...
- CF1102D-Balanced Ternary String-(贪心)
http://codeforces.com/problemset/problem/1102/D 题意: 有n个字符,只能为012,现要通过变换让012的数量相等,并且使字典序最小. 解题: 由样例可以 ...
- 实现:创建/复制/移动文件API
使用的API: 1.DeleteFile DeleteFileW( _In_ LPCWSTR lpFileName ); 2.CopyFile CopyFileW( _In_ LPCWSTR lpEx ...