Apache

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(\d{1,3}\.){3}\d{1,3}$
RewriteRule ^(.*)$ - [F,L]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.aaa.com/$1 [R,L]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]  

RewriteCond %{HTTP_USER_AGENT} ^$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*Discuz_phpwind.* [NC]
RewriteRule .* - [F]

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

第一个是禁止通过服务器的ip地址来访问网站

第二个是强制从http跳转到https,2种写法第二种更好

第三个是禁止相应useragent访问,结果是500

第四个是实现不带www的跳转到www域名


最近遇到一个奇怪的,注意index.php后面的?,不加问号不生效。。。


  Options +FollowSymlinks
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]

另外遇到一种是提示file not found,修改为这样就可以


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L,E=PATH_INFO:$1]


Nginx

if ($http_accept_language ~* ^zh) {
    rewrite ^(.*)$ http://www.aaa.com;
}
if ($http_accept_language !~* ^zh) {
    rewrite ^(.*)$ http://www.bbb.com;
}

判断客户端浏览器语言,访问对应的网站

set $h '';
        if ($host = "store.xxx.com") {
                set $h P;
        }
        if ($host != 'store.xxx.com.cn') {
                set $h "${h}C";
        }
        if ($h = PC) {
                rewrite ^(.*)$  https://$host permanent;
        }

判断客户端浏览器语言,以及ip

set $h '';
if ($http_accept_language ~* ^zh) {
    set $h I;
}
if ($remote_addr ~ !(58.255.190.157|27.41.211.179|221.5.29.136|222.178.71.153)) {
    set $h "${h}P";
}
if ($h = IP) {
        return 404;
}

双重判断

if ($server_port = 80) {
    rewrite ^(.*)$ https://$host$1 permanent;
    }

http跳转https

if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )
	{
		rewrite ^/(.*)$ http://m.xxx.com/$1 permanent;
	}

判断是否是手机端访问

               if ($http_host !~ "^www.xxx.com$") {
                       rewrite  ^(.*)    http://www.xxx.com$1 permanent;
                 }

访问不带www的网址跳转到带www的网址