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]
第一個是禁止通過服務器的ip地址來訪問網站
第二個是強制從http跳轉到https,2種寫法第二種更好
第三個是禁止相應useragent訪問,結果是500
最近遇到一個奇怪的,注意index.php後面的?,不加問號不生效。。。
<IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L] </IfModule>
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.abart.com") { set $h P; } if ($host != 'store.abart.com.cn') { set $h "${h}C"; } if ($h = PC) { rewrite ^(.*)$ https://$host permanent; }
雙重判斷
if ($server_port = 80) { rewrite ^(.*)$ https://$host$1 permanent; }
http跳轉https