.htaccess几个简单的应用

jsjcjsjc0条评论 26 次浏览

wordpress根目录有htaccess文件之后,我们只需要在文件内添加下面的代码,就可以实现很多功能,记录一下以后估计还是要用的
带www的跳转至不带www域名

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

不带www的跳转至www域名

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

所有域名跳转到https://www.getpocket.net

RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.getpocket.net/$1 [R=301,L]

对应域名跳转到对应的https

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

如果网站搬家,老域名全部301跳转到新站点,应该是所有域名都会访问www.getpocket.net

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

如果网站搬家,老域名全部301跳转到新站点,只有制定的域名才会跳转到www.getpocket.net

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

重定向Domain.Com/File/File.Php 到 Otherdomain.Com/Otherfile/Other.Php

RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^file/file.php$ http://www.otherdomain.com/otherfile/other.php [R=301,L]

假设你有域名domain-one.com和domain-two.com,并且在服务器根目录有对应文件夹one和two,那么通过下面的改写就能让Apache同时接受者两个域名的请求:

#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ /one/$1 [L]

RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^/two
RewriteRule ^(.*)$ /two/$1 [L]

另外一种办法,绑定disk.getpocket.net 到 disk 子目录

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^disk\.getpocket\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/disk/
RewriteRule ^(.*)$ disk/$1?Rewrite [L,QSA]

用.htaccess绑定域名,并强制只有getpocket.net和www.getpocket.net才可以访问

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !www.getpocket.net [NC]
RewriteCond %{HTTP_HOST} !getpocket.net [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^.* – [F,L]

反向代理

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ https://google.com/$1 [P]

指定域名的反向代理,只有访问g.getpocket.net的时候才会反向代理google

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !g.getpocket.net [NC]
RewriteRule ^(.*)$ https://google.com/$1 [P]

利用.htaccess禁止盗链

#禁止盗链
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?getpocket.net [NC]
RewriteRule .(jpg|jpeg|png|gif)$ - [NC,F,L]

#禁止盗链并显示指定图片
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?getpocket.net [NC]
RewriteRule .(jpg|jpeg|png|gif)$ https://www.getpocket.net/warning.gif [R,L]


#禁止盗链并连接到首页
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !getpocket.net [NC]
RewriteCond %{HTTP_REFERER} !youdao.com [NC]
RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC]
RewriteCond %{HTTP_REFERER} !twitter.com [NC]
RewriteCond %{HTTP_REFERER} !facebook.com [NC]
RewriteCond %{HTTP_REFERER} !xianguo.com [NC]
RewriteCond %{HTTP_REFERER} !google.cn [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteCond %{HTTP_REFERER} !google.com.tw [NC]
RewriteCond %{HTTP_REFERER} !google.com.sg [NC]
RewriteCond %{HTTP_REFERER} !google.com.hk [NC]
RewriteCond %{HTTP_REFERER} !bloglines.com [NC]
RewriteCond %{HTTP_REFERER} !soso.com [NC]
RewriteCond %{HTTP_REFERER} !mail.qq.com [NC]
RewriteCond %{HTTP_REFERER} !cn.bing.com [NC]
RewriteCond %{HTTP_REFERER} !image.baidu.com [NC]
RewriteCond %{HTTP_REFERER} !feedburner.com [NC]
RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
RewriteRule .*\.(jpg|jpeg|png|gif)$ https://www.getpocket.net/ [R,NC,L]

#禁止盗链并显示指定图片
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !getpocket.net [NC]
RewriteCond %{HTTP_REFERER} !youdao.com [NC]
RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC]
RewriteCond %{HTTP_REFERER} !twitter.com [NC]
RewriteCond %{HTTP_REFERER} !facebook.com [NC]
RewriteCond %{HTTP_REFERER} !xianguo.com [NC]
RewriteCond %{HTTP_REFERER} !google.cn [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteCond %{HTTP_REFERER} !google.com.tw [NC]
RewriteCond %{HTTP_REFERER} !google.com.sg [NC]
RewriteCond %{HTTP_REFERER} !google.com.hk [NC]
RewriteCond %{HTTP_REFERER} !bloglines.com [NC]
RewriteCond %{HTTP_REFERER} !soso.com [NC]
RewriteCond %{HTTP_REFERER} !mail.qq.com [NC]
RewriteCond %{HTTP_REFERER} !cn.bing.com [NC]
RewriteCond %{HTTP_REFERER} !image.baidu.com [NC]
RewriteCond %{HTTP_REFERER} !feedburner.com [NC]
RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
RewriteRule .*\.(jpg|jpeg|png|gif)$ https://www.getpocket.net/warning.gif [R,NC,L]


#禁止盗链并显示指定图片
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !getpocket.net [NC]
RewriteCond %{HTTP_REFERER} !youdao.com [NC]
RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC]
RewriteCond %{HTTP_REFERER} !twitter.com [NC]
RewriteCond %{HTTP_REFERER} !facebook.com [NC]
RewriteCond %{HTTP_REFERER} !xianguo.com [NC]
RewriteCond %{HTTP_REFERER} !google.cn [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteCond %{HTTP_REFERER} !google.com.tw [NC]
RewriteCond %{HTTP_REFERER} !google.com.sg [NC]
RewriteCond %{HTTP_REFERER} !google.com.hk [NC]
RewriteCond %{HTTP_REFERER} !bloglines.com [NC]
RewriteCond %{HTTP_REFERER} !soso.com [NC]
RewriteCond %{HTTP_REFERER} !mail.qq.com [NC]
RewriteCond %{HTTP_REFERER} !cn.bing.com [NC]
RewriteCond %{HTTP_REFERER} !image.baidu.com [NC]
RewriteCond %{HTTP_REFERER} !feedburner.com [NC]
RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
RewriteRule .(gif|png|jpg)$ https://www.getpocket.net/warning.gif [R,NC,L]


发表评论

? razz sad evil ! smile oops grin eek shock ??? cool lol mad twisted roll wink idea arrow neutral cry mrgreen