请教 www跳转不带www http跳转到https
军哥,你好,请教一个问题。用WordPress建的站点,域名填写的是 https://domain.com,可以正常访问。现在想要实现301重定向,带www跳转不带www,http跳转到https。
目前已经实现了
https://domain.com 正常访问
http://www.domain.com 跳转到 https://domain.com
https://www.domain.com 跳转到 https://domain.com
只有
http://domain.com 无法跳转到 https://domain.com
配置如下:
server
{
listen 80;
#listen [::]:80;
server_name www.domain.com domain.com ;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/domain.com;
if ($host = 'http://domain.com') {
return 301 https://domain.com$request_uri;
}
include rewrite/wordpress.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
server
{
listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name www.domain.com domain.com ;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/domain.com;
ssl_certificate /usr/local/nginx/conf/ssl/domain.com/fullchain.cer;
ssl_certificate_key /usr/local/nginx/conf/ssl/domain.com/domain.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "XXXXXXXXXXX";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
include rewrite/wordpress.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
谢谢!
wordpress 应该是自带跳转的,用不带www域名安装的,www域名访问好像是会自动跳转到不带www的域名上
$host 获取到的就是域名不带http://
另外wordpress不需要开pathinfo,开了会导致错误
本帖最后由 null 于 2020-3-2 15:35 编辑
对于http转https,建议采用
#强制http跳转到https
return 301 https://$host$request_uri;
故而你的conf文件应当先在80端口将http转化为https,再在443端口将www跳转到@
server
{
listen 80;
#listen [::]:80;
server_name www.domain.com domain.com ;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/domain.com;
#http全部301跳转到https
return 301 https://$host$request_uri;
include rewrite/wordpress.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
server
{
listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name www.domain.com domain.com ;
index index.html index.htm index.php default.html default.htm default.php;
root/home/wwwroot/domain.com;
#去掉www
if( $host != 'www.domain.com'){
rewrite ^/(.*)$ https://domain.com/$1 permanent;
}
ssl_certificate /usr/local/nginx/conf/ssl/domain.com/fullchain.cer;
ssl_certificate_key /usr/local/nginx/conf/ssl/domain.com/domain.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "XXXXXXXXXXX";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
include rewrite/wordpress.conf;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log off;
}
licess 发表于 2020-2-29 16:56
wordpress 应该是自带跳转的,用不带www域名安装的,www域名访问好像是会自动跳转到不带www的域名上
$host ...
谢谢军哥 null 发表于 2020-3-2 15:34
对于http转https,建议采用
故而你的conf文件应当先在80端口将http转化为https,再在443端口将www跳转到@
谢谢,我试下看看。
页:
[1]