VPS侦探论坛

 找回密码
 注册
查看: 4398|回复: 4

请教 www跳转不带www http跳转到https

[复制链接]
发表于 2020-2-29 10:03:58 | 显示全部楼层 |阅读模式

军哥,你好,请教一个问题。用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


配置如下:
  1. server
  2.     {
  3.         listen 80;
  4.         #listen [::]:80;
  5.         server_name www.domain.com domain.com ;
  6.         index index.html index.htm index.php default.html default.htm default.php;
  7.         root  /home/wwwroot/domain.com;
  8.                 if ($host = 'http://domain.com') {
  9.                           return 301 https://domain.com$request_uri;
  10.                 }

  11.         include rewrite/wordpress.conf;
  12.         #error_page   404   /404.html;

  13.         # Deny access to PHP files in specific directory
  14.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  15.         include enable-php-pathinfo.conf;

  16.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  17.         {
  18.             expires      30d;
  19.         }

  20.         location ~ .*\.(js|css)?$
  21.         {
  22.             expires      12h;
  23.         }

  24.         location ~ /.well-known {
  25.             allow all;
  26.         }

  27.         location ~ /\.
  28.         {
  29.             deny all;
  30.         }

  31.         access_log off;
  32.     }

  33. server
  34.     {
  35.         listen 443 ssl http2;
  36.         #listen [::]:443 ssl http2;
  37.         server_name www.domain.com domain.com ;
  38.         index index.html index.htm index.php default.html default.htm default.php;
  39.         root  /home/wwwroot/domain.com;

  40.         ssl_certificate /usr/local/nginx/conf/ssl/domain.com/fullchain.cer;
  41.         ssl_certificate_key /usr/local/nginx/conf/ssl/domain.com/domain.com.key;
  42.         ssl_session_timeout 5m;
  43.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  44.         ssl_prefer_server_ciphers on;
  45.         ssl_ciphers "XXXXXXXXXXX";
  46.         ssl_session_cache builtin:1000 shared:SSL:10m;
  47.         # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
  48.         ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

  49.         include rewrite/wordpress.conf;
  50.         #error_page   404   /404.html;

  51.         # Deny access to PHP files in specific directory
  52.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  53.         include enable-php-pathinfo.conf;

  54.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  55.         {
  56.             expires      30d;
  57.         }

  58.         location ~ .*\.(js|css)?$
  59.         {
  60.             expires      12h;
  61.         }

  62.         location ~ /.well-known {
  63.             allow all;
  64.         }

  65.         location ~ /\.
  66.         {
  67.             deny all;
  68.         }

  69.         access_log off;
  70.     }
复制代码



谢谢!
美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
发表于 2020-2-29 16:56:29 | 显示全部楼层


wordpress 应该是自带跳转的,用不带www域名安装的,www域名访问好像是会自动跳转到不带www的域名上

$host 获取到的就是域名不带http://

另外wordpress不需要开pathinfo,开了会导致错误
Linux下Nginx+MySQL+PHP自动安装工具:https://lnmp.org
发表于 2020-3-2 15:34:09 | 显示全部楼层

本帖最后由 null 于 2020-3-2 15:35 编辑

对于http转https,建议采用
  1. #强制http跳转到https
  2. return 301 https://$host$request_uri;
复制代码

故而你的conf文件应当先在80端口将http转化为https,再在443端口将www跳转到@

  1. server
  2.     {
  3.         listen 80;
  4.         #listen [::]:80;
  5.         server_name www.domain.com domain.com ;
  6.         index index.html index.htm index.php default.html default.htm default.php;
  7.         root  /home/wwwroot/domain.com;
  8.         #http全部301跳转到https
  9.         return 301 https://$host$request_uri;
  10.         
  11.         include rewrite/wordpress.conf;
  12.         #error_page   404   /404.html;

  13.         # Deny access to PHP files in specific directory
  14.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  15.         include enable-php-pathinfo.conf;

  16.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  17.         {
  18.             expires      30d;
  19.         }

  20.         location ~ .*\.(js|css)?$
  21.         {
  22.             expires      12h;
  23.         }

  24.         location ~ /.well-known {
  25.             allow all;
  26.         }

  27.         location ~ /\.
  28.         {
  29.             deny all;
  30.         }

  31.         access_log off;
  32.     }

  33. server
  34.     {
  35.         listen 443 ssl http2;
  36.         #listen [::]:443 ssl http2;
  37.         server_name www.domain.com domain.com ;
  38.         index index.html index.htm index.php default.html default.htm default.php;
  39.         root  /home/wwwroot/domain.com;
  40.         #去掉www
  41.         if  ( $host != 'www.domain.com'  )  {
  42.                         rewrite ^/(.*)$ https://domain.com/$1 permanent;
  43.                 }

  44.         ssl_certificate /usr/local/nginx/conf/ssl/domain.com/fullchain.cer;
  45.         ssl_certificate_key /usr/local/nginx/conf/ssl/domain.com/domain.com.key;
  46.         ssl_session_timeout 5m;
  47.         ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  48.         ssl_prefer_server_ciphers on;
  49.         ssl_ciphers "XXXXXXXXXXX";
  50.         ssl_session_cache builtin:1000 shared:SSL:10m;
  51.         # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
  52.         ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

  53.         include rewrite/wordpress.conf;
  54.         #error_page   404   /404.html;

  55.         # Deny access to PHP files in specific directory
  56.         #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

  57.         include enable-php-pathinfo.conf;

  58.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  59.         {
  60.             expires      30d;
  61.         }

  62.         location ~ .*\.(js|css)?$
  63.         {
  64.             expires      12h;
  65.         }

  66.         location ~ /.well-known {
  67.             allow all;
  68.         }

  69.         location ~ /\.
  70.         {
  71.             deny all;
  72.         }

  73.         access_log off;
  74.     }
复制代码


美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
 楼主| 发表于 2020-7-5 22:26:26 | 显示全部楼层



licess 发表于 2020-2-29 16:56
wordpress 应该是自带跳转的,用不带www域名安装的,www域名访问好像是会自动跳转到不带www的域名上

$host ...

谢谢军哥
Linux下Nginx+MySQL+PHP自动安装工具:https://lnmp.org
 楼主| 发表于 2020-7-5 22:26:37 | 显示全部楼层

null 发表于 2020-3-2 15:34
对于http转https,建议采用

故而你的conf文件应当先在80端口将http转化为https,再在443端口将www跳转到@

谢谢,我试下看看。

军哥运维代购:http://shop63846532.taobao.com/

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|VPS侦探 ( 鲁ICP备16040043号-1 )

GMT+8, 2024-10-18 13:38 , Processed in 0.027703 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表