Nginx网站开启SSL后将带www的域名和http协议的网址全部301跳转到不带www的https域名的方法
很多朋友给网站开启SSL后,没有完全设置好301跳转,造成搜索引擎收录了各种各样的URL,非常不利于SEO,因为《百度搜索引擎网页质量白皮书》里要求每一个页面只对应一条唯一的URL。本文分享的就是是将http://zhangwenbao.com、http://www.zhangwengbao.com和https://www.zhangwenbao.com三种形式的网址完美跳转到https://zhangwenbao.com的方法。本方法在Nginx 1.10.1下测试成功,也是保哥笔记正在使用的代码分享给大家,一共分为三部分。
第一部分代码是将http://zhangwenbao.com和http://www.zhangwenbao.com跳转到https://zhangwenbao.com,代码如下:
server {
listen 80;
server_name zhangwenbao.com www.zhangwenbao.com;
root /web/www/zhangwenbao_com;
return 301 https://zhangwenbao.com$request_uri;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
# {{{ block/deny
## Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
return 403;
}
## Block some robots ##
if ($http_user_agent ~* msnbot|scrapbot) {
return 403;
}
## Deny certain Referers ###
if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) ) {
return 403;
}
# }}}
location / {
index index.php index.html index.htm;
}
location = /50x.html {
root html;
}
location ~* apple-touch-icon {
access_log off;
rewrite .* /fav-icon.png last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
access_log /web/log/nginx/default.access.log dynamic;
}
第二部分代码,是开启ssl端口,将https://zhangwenbao.com域名绑443端口:
server {
listen 443;
server_name zhangwenbao.com;
ssl on;
root /web/www/zhangwenbao_com;
index index.html index.htm;
ssl_certificate cert/zhangwenbao_com.pem;
ssl_certificate_key cert/zhangwenbao_com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
# {{{ block/deny
## Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
return 403;
}
## Block some robots ##
if ($http_user_agent ~* msnbot|scrapbot) {
return 403;
}
## Deny certain Referers ###
if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) ) {
return 403;
}
# }}}
location / {
index index.php index.html index.htm;
}
location = /50x.html {
root html;
}
location ~* apple-touch-icon {
access_log off;
rewrite .* /fav-icon.png last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
access_log /web/log/nginx/default.access.log dynamic;
}
第三部分代码是将https://www.zhangwenbao.com也绑定443端口,同时做301跳转到https://zhangwenbao.com:
server {
listen 443;
server_name www.zhangwenbao.com;
return 301 https://zhangwenbao.com$request_uri;
ssl on;
root /web/www/zhangwenbao_com;
index index.html index.htm;
ssl_certificate cert/zhangwenbao_com.pem;
ssl_certificate_key cert/zhangwenbao_com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
# {{{ block/deny
## Block download agents ##
if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
return 403;
}
## Block some robots ##
if ($http_user_agent ~* msnbot|scrapbot) {
return 403;
}
## Deny certain Referers ###
if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) ) {
return 403;
}
# }}}
location / {
index index.php index.html index.htm;
}
location = /50x.html {
root html;
}
location ~* apple-touch-icon {
access_log off;
rewrite .* /fav-icon.png last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
access_log /web/log/nginx/default.access.log dynamic;
}
将上面保哥提供的三个部分的代码全部写入你网站的.conf文件里,保存后重启Nginx即可生效。记得注意修改为你的网站域名、网站目录路径、SSL证书路径和SSL证书文件名。
因本文不是用Markdown格式的编辑器书写的,转换的页面可能不符合AMP标准。