要将HTTP流量重定向到HTTPS,可以通过在服务器配置文件中添加以下代码来实现:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
const express = require('express');
const app = express();
app.use((req, res, next) => {
if (!req.secure) {
return res.redirect(`https://${req.headers.host}${req.url}`);
}
next();
});
// 其他路由和中间件设置
app.listen(80, () => {
console.log('Server started on port 80');
});
请注意,这些代码示例中的域名和端口号可能需要根据您自己的配置进行修改。