如何将非 www 重定向到 www 并将 http 重定向到 https?
How to redirect non www to www and http to https?
我用 React nodejs 创建了一个小型网络应用程序。我托管在 IIS Web 服务器上。我想将非 www URL 重定向到 www 并将 HTTP 重定向到 HTTPS。我在 web.config 文件下面使用了重定向
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
<rule name="httptohttps" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="non-wwwtowww" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
以上代码对着陆页有效,但对其他页面 HTTPS 无效。我已经购买了SSL证书。
如果我直接打开示例。com/xyz页面那么非安全页面打开意味着http://example.com/xyz页面打开。
如果我从 URL 中删除了 s 和 www,意思是 https://www.example.com/xyz 则不会执行路由。
我想将我的 Web 应用程序的所有页面路由到 HTTPS 和 www。
我在服务器端(node js)写了一些代码
//301 redirection http to https and non-www to www
const redirectionFilter = function (req, res, next) {
const theDate = new Date();
const receivedUrl = `${req.protocol}:\/\/${req.hostname}:${port}${req.url}`;
if (req.get('X-Forwarded-Proto') === 'http') {
const redirectTo = `https:\/\/${req.hostname}${req.url}`;
console.log(`${theDate} Redirecting ${receivedUrl} --> ${redirectTo}`);
res.redirect(301, redirectTo);
} else {
next();
}
};
/**
* Apply redirection filter to all requests
*/
app.get('/*', redirectionFilter);
app.use((req, res, next) => {
const host = req.header('host');
if (host.match(/^www\..*/i)) {
next();
} else {
res.redirect(301, `${req.protocol}://www.${host}${req.url}`);
}
});
您可以试试这个规则,将它们合并为一个并使用 logicalGrouping 作为条件并将其设置为 Any,这相当于“或”。
<rule name="Force WWW and HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
我用 React nodejs 创建了一个小型网络应用程序。我托管在 IIS Web 服务器上。我想将非 www URL 重定向到 www 并将 HTTP 重定向到 HTTPS。我在 web.config 文件下面使用了重定向
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
<rule name="httptohttps" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="non-wwwtowww" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
以上代码对着陆页有效,但对其他页面 HTTPS 无效。我已经购买了SSL证书。
如果我直接打开示例。com/xyz页面那么非安全页面打开意味着http://example.com/xyz页面打开。
如果我从 URL 中删除了 s 和 www,意思是 https://www.example.com/xyz 则不会执行路由。
我想将我的 Web 应用程序的所有页面路由到 HTTPS 和 www。
我在服务器端(node js)写了一些代码
//301 redirection http to https and non-www to www
const redirectionFilter = function (req, res, next) {
const theDate = new Date();
const receivedUrl = `${req.protocol}:\/\/${req.hostname}:${port}${req.url}`;
if (req.get('X-Forwarded-Proto') === 'http') {
const redirectTo = `https:\/\/${req.hostname}${req.url}`;
console.log(`${theDate} Redirecting ${receivedUrl} --> ${redirectTo}`);
res.redirect(301, redirectTo);
} else {
next();
}
};
/**
* Apply redirection filter to all requests
*/
app.get('/*', redirectionFilter);
app.use((req, res, next) => {
const host = req.header('host');
if (host.match(/^www\..*/i)) {
next();
} else {
res.redirect(301, `${req.protocol}://www.${host}${req.url}`);
}
});
您可以试试这个规则,将它们合并为一个并使用 logicalGrouping 作为条件并将其设置为 Any,这相当于“或”。
<rule name="Force WWW and HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.zzz.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>