Iis 如何提高重定向代码的效率
我在我所有的ASP页面的顶部都有这个包含,可以重定向到https,用于所有不在名为“words”和“generators”文件夹中的页面。我不希望他们使用ssl,因为我听说使用ssl会对AdSense的收入产生很大影响 我还重定向,这样,如果用户最终出现在words和generators文件夹中的SSL版本的页面上,它会将他们带到非SSL页面 最后,我有一个从网页的www版本重定向到非www版本的页面,例如www.test.co.uk到test.co.uk 我的代码如下 我想知道我是不是在以一种资源密集型的方式做这件事,是否有更好的方式使用web配置或类似的东西来做这件事,或者如果它没有损坏,就不要修复它 我意识到我的代码是非常基本和笨重的,对不起Iis 如何提高重定向代码的效率,iis,asp-classic,Iis,Asp Classic,我在我所有的ASP页面的顶部都有这个包含,可以重定向到https,用于所有不在名为“words”和“generators”文件夹中的页面。我不希望他们使用ssl,因为我听说使用ssl会对AdSense的收入产生很大影响 我还重定向,这样,如果用户最终出现在words和generators文件夹中的SSL版本的页面上,它会将他们带到非SSL页面 最后,我有一个从网页的www版本重定向到非www版本的页面,例如www.test.co.uk到test.co.uk 我的代码如下 我想知道我是不是在以一种
SERVER_NAME = lcase(Request.ServerVariables("SERVER_NAME"))
SCRIPT_NAME = lcase(Request.ServerVariables("SCRIPT_NAME"))
QUERY_STRING = lcase(Request.ServerVariables("QUERY_STRING"))
SECURE_MODE = lcase(Request.ServerVariables("SERVER_PORT_SECURE"))
str0 = request.servervariables("url")
arrFolderData = Split(str0, "/")
strLastFolder = arrFolderData(UBound(arrFolderData)-1)
words_str = instr(strLastFolder,"words")
gens_str = instr(strLastFolder,"generators")
'' if page = http, and page is not in "words" or "generators" folder then redirect to https version of page
if SECURE_MODE = 0 AND words_str = 0 AND gens_str = 0 then
SERVER_NAME = replace(SERVER_NAME, "www.", "")
go_to_url = ""
go_to_url = go_to_url & "https://"
go_to_url = go_to_url & SERVER_NAME
go_to_url = go_to_url & SCRIPT_NAME
if QUERY_STRING <> "" then
go_to_url = go_to_url & "?" & QUERY_STRING
end if
Response.Buffer = true
Response.Status = "301 Redirect"
Response.AddHeader "Location", lcase(go_to_url)
Response.End
end if
'' if page = https, and page is in "words" or "generators" folder then redirect to http version of page
if SECURE_MODE = 1 AND (words_str = 1 OR gens_str = 1) then
SERVER_NAME = replace(SERVER_NAME, "www.", "")
go_to_url = ""
go_to_url = go_to_url & "http://"
go_to_url = go_to_url & SERVER_NAME
go_to_url = go_to_url & SCRIPT_NAME
if QUERY_STRING <> "" then
go_to_url = go_to_url & "?" & QUERY_STRING
end if
Response.Buffer = true
Response.Status = "301 Redirect"
Response.AddHeader "Location", lcase(go_to_url)
Response.End
end if
'' redirect to non "www" version of page
if left(SERVER_NAME,3) = "www" then
SERVER_NAME = replace(SERVER_NAME, "www.", "")
go_to_url = ""
go_to_url = go_to_url & "http://"
go_to_url = go_to_url & SERVER_NAME
go_to_url = go_to_url & SCRIPT_NAME
if QUERY_STRING <> "" then
go_to_url = go_to_url & "?" & QUERY_STRING
end if
Response.Buffer = true
Response.Status = "301 Redirect"
Response.AddHeader "Location", lcase(go_to_url)
Response.End
end if
SERVER\u NAME=lcase(Request.ServerVariables(“SERVER\u NAME”))
SCRIPT\u NAME=lcase(Request.ServerVariables(“SCRIPT\u NAME”))
QUERY\u STRING=lcase(Request.ServerVariables(“QUERY\u STRING”))
SECURE\u MODE=lcase(Request.ServerVariables(“SERVER\u PORT\u SECURE”))
str0=request.servervariables(“url”)
arrFolderData=拆分(str0,“/”)
strLastFolder=arrFolderData(UBound(arrFolderData)-1)
words_str=instr(strLastFolder,“words”)
gens_str=instr(strLastFolder,“发电机”)
“”如果页面=http,并且页面不在“Word”或“generators”文件夹中,则重定向到页面的https版本
如果SECURE_MODE=0,words_str=0,gens_str=0,则
服务器名称=替换(服务器名称,“www.,”)
go_to_url=“”
go_to_url=go_to_url&“https://”
go_to_url=go_to_url和服务器名称
go_to_url=go_to_url和脚本名称
如果查询字符串为“”,则
go_to_url=go_to_url&“?”&查询字符串
如果结束
Response.Buffer=true
Response.Status=“301重定向”
Response.AddHeader“Location”,lcase(转到url)
答复.完
如果结束
“”如果page=https,且页面位于“words”或“generators”文件夹中,则重定向到页面的http版本
如果安全模式=1且(单词\u str=1或gens\u str=1),则
服务器名称=替换(服务器名称,“www.,”)
go_to_url=“”
go_to_url=go_to_url&“http://”
go_to_url=go_to_url和服务器名称
go_to_url=go_to_url和脚本名称
如果查询字符串为“”,则
go_to_url=go_to_url&“?”&查询字符串
如果结束
Response.Buffer=true
Response.Status=“301重定向”
Response.AddHeader“Location”,lcase(转到url)
答复.完
如果结束
“”重定向到页面的非“www”版本
如果左(服务器名称,3)=“www”,则
服务器名称=替换(服务器名称,“www.,”)
go_to_url=“”
go_to_url=go_to_url&“http://”
go_to_url=go_to_url和服务器名称
go_to_url=go_to_url和脚本名称
如果查询字符串为“”,则
go_to_url=go_to_url&“?”&查询字符串
如果结束
Response.Buffer=true
Response.Status=“301重定向”
Response.AddHeader“Location”,lcase(转到url)
答复.完
如果结束
感谢@Carlos Aguilar Mares的帮助,我能够用以下代码替换上述代码:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical HostName" stopProcessing="true">
<!-- Redirect to the non-www host -->
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="NON HTTPS" enabled="true" stopProcessing="true">
<!-- Redirect to HTTPS as long as pages are not in words, generators or v folders -->
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{REQUEST_URI}" pattern="^/words/[a-z 0-9]*" negate="true" />
<add input="{REQUEST_URI}" pattern="^/generators/[a-z 0-9]*" negate="true" />
<add input="{REQUEST_URI}" pattern="^/v/[a-z 0-9]*" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
是的,如果在web.config中使用URL重写和配置,这可以很容易地完成,而且可能会更快(不再使用脚本,不再在本机代码中解析代码、正则表达式和逻辑,加上更易于维护等) 规则如下所示:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="NON HTTPS" enabled="true" stopProcessing="true">
<!-- Redirect to HTTPS as long as it does not end in words/generators -->
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{URL}" pattern="(words|generators)$" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="HTTPS" enabled="true" stopProcessing="true">
<!-- Redirect to HTTPS as long as it ends in words/generators -->
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="on" />
<add input="{URL}" pattern="(words|generators)$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Canonical HostName" stopProcessing="true">
<!-- Redirect to the non-www host -->
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
谢谢@Carlos Aguilar Mares!