一:windows操作系统,在网站根目录下,新建web.config这个文件
1:不带www跳转到www代码示例
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="to www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^sznest\.net$" />
</conditions>
<action type="Redirect" url="http://www.sznest.net/{R:0}" />
</rule>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
2:http跳转到https代码示例
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
也可以用下面的
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" ></match>
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_FROM_HTTPS}" pattern="^on$" negate="true" ></add>
</conditions>
<action type="Redirect" url="https://www.顶级域名/{R:1}" redirectType="Permanent" ></action>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
3:不带www跳转到www,http跳转到https代码示例
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="to www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^sznest\.net$" />
</conditions>
<action type="Redirect" url="http://www.sznest.net/{R:0}" />
</rule>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
二:Linux操作系统,在网站根目录下,新建.htaccess这个文件
1:不带www跳转到www代码示例
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sznest.net [NC]
RewriteRule ^(.*)$ http://www.sznest.net/$1 [L,R=301]
</IfModule>
2:http跳转到https代码示例
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>
3:不带www跳转到www,http跳转到https代码示例
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sznest.net [NC]
RewriteRule ^(.*)$ http://www.sznest.net/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.sznest.net/$1 [L,R=301]
</IfModule>