htaccess to nginx converter:
https://www.bt.cn/Tools
http://winginx.com/en/htaccess
有绑定多个域名非cloud.cloudduo.cn的话,跳转到cloud.cloudduo.cn
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cloud.cloudduo.cn [NC]
RewriteRule ^(.*) http://cloud.cloudduo.cn [L,R=301]
</IfModule
强制https
#HTTP_TO_HTTPS_START
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}$1 [L,R=301]
</IfModule>
#HTTP_TO_HTTPS_END
整站转到301到 https://www.cloudduo.cn
#301-START
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ https://www.cloudduo.cn/ [L,R=301]
</IfModule>
强制www域名301跳转
我们将域名替换成自己的域名即可实现非WWW跳转到WWW域名。
RewriteEngine on
RewriteCond %{HTTP_HOST} ^niaoyun\.com [NC]
RewriteRule ^(.*)$ http://www.niaoyun.com/$1 [L,R=301,NC]
强制非WWW域名301跳转
这里我们可以强制如果访问www域名前缀,会跳转到非www域名。
RewriteEngine On
RewriteCond %{HTTP_HOST} !^niaoyun.com$ [NC]
RewriteRule ^(.*)$ http://niaoyun.com/$1 [L,R=301]
强制https跳转地址。
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.nioayun.com/$1 [R=30,L]
如何我们网站采用SSL证书,则一般需要强制跳转https路径,这里需要添加强制80端口的跳转。
强制带WWW跳转和HTTPS强制不带WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} !^cnbanwagong.com$ [NC]
RewriteRule ^(.*)$ https://cnbanwagong.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://cnbanwagong.com/$1 [L,R=301]
如果我们有朋友喜欢用不带WWW的格式,所以我们也可以强制带WWW跳转和HTTPS强制不带WWW。
阻止无用的蜘蛛爬取。
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 http://www.niaoyun.com/b.html
阻止某个IP或则某个网段访问。
参考官方教程:https://help.aliyun.com/knowledge_detail/36226.html
Order deny,allow
Allow from all
Deny from xxx.xxx.xxx.xxx
Deny from xxx.xxx.xxx.
禁止图片盗链。
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(.+\.)?niaoyun.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
常用和不常用的.htaccess伪静态、301、防盗链以及等设置功能。如果需要更多强大的htaccess功能则需要阅读官方文档,这10个用途仅仅是很小的部分。
防盗链
RewriteCond %{REQUEST_URI} ^/anti
RewriteRule ^.*$ - [L]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|bmp|zip|rar|mp3|txt|png|wma|wmv)$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !www.bbsfh.cn [NC]
RewriteCond %{HTTP_REFERER} !bbsfh.cn [NC]
RewriteCond %{HTTP_REFERER} !122.227.166.242 [NC]
RewriteCond %{HTTP_REFERER} !www.google.com [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteCond %{HTTP_REFERER} !www.google.com.hk [NC]
RewriteCond %{HTTP_REFERER} !google.com.hk [NC]
RewriteCond %{HTTP_REFERER} !www.google.cn [NC]
RewriteCond %{HTTP_REFERER} !google.cn [NC]
RewriteCond %{HTTP_REFERER} !www.baidu.com [NC]
RewriteCond %{HTTP_REFERER} !www.baidu.cn [NC]
RewriteCond %{HTTP_REFERER} !baidu.com [NC]
RewriteCond %{HTTP_REFERER} !baidu.cn [NC]
RewriteCond %{HTTP_REFERER} !so.com [NC]
RewriteCond %{HTTP_REFERER} !www.so.com [NC]
RewriteCond %{HTTP_REFERER} !www.163.com [NC]
RewriteCond %{HTTP_REFERER} !www.yahoo.com [NC]
RewriteCond %{HTTP_REFERER} !www.sogou.com [NC]
RewriteCond %{HTTP_REFERER} !www.soso.com [NC]
RewriteCond %{HTTP_REFERER} !www.sina.com.cn [NC]
RewriteCond %{HTTP_REFERER} !www.msn.com [NC]
RewriteCond %{HTTP_REFERER} !sina.com.cn [NC]
RewriteCond %{HTTP_REFERER} !bing.com [NC]
RewriteCond %{HTTP_REFERER} !www.bing.com [NC]
RewriteRule (.*) /anti/sorry.gif [R,NC,L]