[英]Force www to https in Express NodeJS
I build a NodeJS app with express and in Firefox when I type domain name.com it's not redirecting to https://www.<domain_name>.com我用 express 构建了一个 NodeJS 应用程序,当我输入域名时,在 Firefox 中。com 它不会重定向到 https://www.<domain_name>.com
Can someone help on how to redirect <domain_name>.com and www.<domain_name>.com with https?有人可以帮助解决如何使用 https 重定向 <domain_name>.com 和 www.<domain_name>.com 吗?
let host = req.headers.host;
if (!host.match(/^www\..*/i)) {
return res.redirect(301, "https://www." + host + req.url);
}
next();
}
I tried the above one but no luck on Firefox.我尝试了上面的方法,但在 Firefox 上没有成功。
You have to edit your http server configuration accordingly:您必须相应地编辑您的 http 服务器配置:
apache: etc/apache2/sites-available/000-default
change to this:更改为:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
or或者
nginx: etc/nginx/sites-available/default
change to this:更改为:
server {
listen 80;
server_name your_domain.com www.your_domain.com;
return 301 https://$host$request_uri;
}
If you still experience issues, edit .htaccess file:如果您仍然遇到问题,请编辑 .htaccess 文件:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?supergenscript\.com
RewriteRule ^(.*)$ https://www.supergenscript.com/$1 [R=301, L]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.