考虑到用户体验,网站通常会一个跳转。比如我的网站域名是 liuhaolin.com
,网站的首页是 https://www.liuhaolin.com
, 如果web服务器用的是iis,那么操作就比较简单。通常修改一下 web.config
文件就可以。
iis重定向到www的方法
在网站的更目录,添加一条规则
<rule name="Redirect to www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^liuhaolin.com$" />
</conditions>
<action type="Redirect" url="https://www.liuhaolin.com/{R:0}" redirectType="Permanent" />
</rule>
类似的跳转,还有一个,就是 http
跳转到 https
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true"><match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" />
</rule>
iis中 wordpress 的重写规则
<rule name="WordPress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
已上,三条规则是在处理一个wordpress网站遇到的问题。总结一下,会贴出一个完整的配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^liuhaolin.com$" />
</conditions>
<action type="Redirect" url="https://www.liuhaolin.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true"><match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" />
</rule>
<rule name="WordPress" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>