小冬SEO

伪静态规则环境配置apache环境.htaccess301技巧

2019-08-16 14:49:16 5133 织梦安全

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功能则需要阅读官方文档。

Apache Rewrite规则简介:
Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言。可基于服务器级的(httpd.conf)和目录级的(.htaccess)两种方式。如果要想用到rewrite模块,必须先安装或加载rewrite模块。方法有两种一种是编译apache的时候就直接安装rewrite模块,别一种是编译apache时以DSO模式安装apache,然后再利用源码和apxs来安装rewrite模块。
基于服务器级的(httpd.conf)有两种方法,一种是在httpd.conf的全局下直接利用RewriteEngine on来打开rewrite功能;另一种是在局部里利用RewriteEngine on来打开rewrite功能,下面将会举例说明,需要注意的是,必须在每个virtualhost里用RewriteEngine on来打开rewrite功能。否则virtualhost里没有RewriteEngine on它里面的规则也不会生效。
基于目录级的(.htaccess),要注意一点那就是必须打开此目录的FollowSymLinks属性且在.htaccess里要声明RewriteEngine on。

Apache mod_rewrite规则重写的标志一览
1) R[=code](force redirect) 强制外部重定向
强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。

2) F(force URL to be forbidden)禁用URL,返回403HTTP状态码。

3) G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。

4) P(force proxy) 强制使用代理转发。

5) L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。

6) N(next round) 重新从第一条规则开始运行重写过程。

7) C(chained with next rule) 与下一条规则关联
如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。

8) T=MIME-type(force MIME type) 强制MIME类型

9) NS (used only if no internal sub-request) 只用于不是内部子请求

10) NC(no case) 不区分大小写

11) QSA(query string append) 追加请求字符串

12) NE(no URI escaping of output) 不在输出转义特殊字符
例如:RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zoo

13) PT(pass through to next handler) 传递给下一个处理
例如:
RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理
Alias /def /ghi
14) S=num(skip next rule(s)) 跳过num条规则
15) E=VAR:VAL(set environment variable) 设置环境变量

301跳转的实现是通过编辑网站主目录下的301jump文件来实现的。
本地保留一份与服务器上一样的文件(301jump),需要设置301跳转的时候可以通过编辑这个文件(添加需要的301跳转或者去除不需要的301跳转),然后通过FTP上传到网站目录下面(test的服务器上),覆盖掉以前的文件。
然后通过test.域名.com的地址访问,测试301跳转是否生效。生效后同步到远程服务器即可。
301jump 文件的格式如下:

RewriteEngine on
RewriteRule  ^a.html  /b.html  [R=301,L]
RewriteRule XXXXXXX XXXXXXX [R=301,L]
RewriteRule XXXXXXX XXXXXXX [R=301,L]
……………………………………..

下面介绍介绍跳转规则
第一行RewriteEngine on是开启跳转功能的配置

第二行开始
如:RewriteRule XXXXXXX XXXXXXX [R=301,L] 格式的是跳转
每一行表示一个跳转

我们的设置只要在文件末尾添加要设置的跳转,或者删掉要去除的跳转

跳转情况说明:(说明均是以chinasoft.com站点下的301jump 编写为例)

情况一
http://www.chinasoft.com/a.html   跳转到 http://www.chinasoft.com/b.html
让站点下面的一个页面跳转到另一个页面

写法如下:
RewriteRule  ^a.html  /b.html  [R=301,L]

情况二
http://www.chinasoft.com/a.html   跳转到 http://www.chinasoft.com/abc/b.html
让站点下面的一个页面跳转到非同级目录的另一个页面

写法如下:
RewriteRule  ^a.html  /abc/b.html  [R=301,L]

http://www.chinasoft.com/abc/a.html   跳转到 http://www.chinasoft.com/b.html
让站点下面的一个页面跳转到非同级目录的另一个页面

写法如下:
RewriteRule  ^abc/a.html  /b.html  [R=301,L]

情况三
http://www.chinasoft.com/word/index.html  跳转到 http://www.chinasoft.com/word/
让chinasoft.com 站点下的/word/index.html文件夹的链接重新定向到 /word/ 目录,seo优化

写法如下:
RewriteRule  ^word/index.html /word/   [R=301,L]

情况四
http://www.chinasoft.com/word/   跳转到 http://www.chinasoft.com/sentence/
让chinasoft.com 站点下的/word/文件夹的链接重新定向到/sentence/下面,包含下层路径
例如:http://www.chinasoft.com/word/a.php
会被定向到http://www.chinasoft.com/sentence/a.php
写法如下:
RewriteRule  ^word/(.*)$  /sentence/$1  [R=301,L]

情况五
http://www.chinasoft.com/word/   跳转到 http://www.chinasoft.com
让chinasoft.com 站点下的/word文件夹的链接重新定向到 根目录 下面,包含下层路径
例如:http://www.chinasoft.com/word/a.php
会被定向到http://www.chinasoft.com/a.php
写法如下:
RewriteRule  ^word/(.*)$  /$1  [R=301,L]

情况六
http://www.chinasoft.com/a.html 跳转到 http://www.daniusoft.com/b.html
非相同域名站点的跳转

写法如下:
RewriteRule  ^a.html   http://www.daniusoft.com/b.html [R=301,L]

情况七
http://www.chinasoft.com   跳转到 http://www.daniusoft.com
让chinasoft.com 站点链接重新定向到 www.daniusoft.com

写法如下:
RewriteRule  (.*)$  http://www.daniusoft.com/$1  [R=301,L]
注意:这里daniusoft.com后面的 / 一定不能漏掉,如果漏掉的话,会出现如下错误:
chinasoft.com/a.html 跳转地址会是 daniusoft.coma.html  (该地址不存在)

情况八
http://www.chinasoft.com下的所有文件   跳转到 http://www.daniusoft.com首页
让chinasoft.com 站点所有页面重新定向到 www.daniusoft.com首页

写法如下:
RewriteRule  (.*)$  http://www.daniusoft.com  [R=301,L]

情况九
动态页面取值跳转
support.iskysoft.com/index.php?submod=order&method=retrieve页面
跳到指定页面
support.iskysoft.com/contact-support/retrieve-registration-code.html

写法如下:
^/index.php /aaa/%{QUERY_STRING}? [R=301,L]
^/aaa/submod=order&method=retrieve /contact-support/retrieve-registration-code.html [R=301,L]

小技巧:
如果需要让一条跳转不生效,暂时又不想删除此条跳转,可能以后还会用到。
在此条跳转前面加一个”#”,表示注释掉,此跳转不生效。
例如:
RewriteEngine on
#RewriteRule  ^a.html  /b.html  [R=301,L]
RewriteRule XXXXXXX XXXXXXX [R=301,L]
RewriteRule XXXXXXX XXXXXXX [R=301,L]
站点中a.html到b.html的跳转将不会生效。

生效原则:先后顺序

跳转遵从从上往下优先匹配原则。
一旦前面有覆盖后面跳转的跳转存在,以前面跳转为准
例1:
RewriteEngine on
RewriteRule  ^a.html  /b.html  [R=301,L]
RewriteRule  ^a.html  /c.html   [R=301,L]
RewriteRule  ^a.html  /d.html  [R=301,L]
站点中a.html到b.html的跳转将会生效,其他两条不生效。
例2:
RewriteEngine on
RewriteRule  ^word/ (.*)$  /sentence/$1  [R=301,L]
RewriteRule  ^word/a.html   /a.html   [R=301,L]
优先匹配  word/ 到sentence/的跳转。

由于历史原因,我们许多站点存在基于配置文件级的301跳转,这些跳转均优先于我们所编辑的301jump文件的跳转。

以下情况:
1、设置的跳转不生效。
2、跳转莫名奇妙的跳转到其他地址。
3、想取消一个跳转规则,怎么也找不到。

 
如果有大规模跳转需要设置(一次在50条以上),把需要跳转的页面粘贴到新建文件1,跳转到目标页面的粘贴到文件2。

cat -n 1 >3
cat -n 2 >4
join -o 1.2,2.2 3 4 >5

完整站点示例:

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

版权保护: 本文由小冬SEO编辑发布,转载请保留链接: http://www.myseoyh.cn/cms/dedesafe/67.html