我在所有ASP页面的顶部都有这个包含,重定向到所有页面的https,不在名为“words”和“generators”的文件夹中。我不希望他们在ssl上,因为我听说转向SSL会对AdSense收入产生重大影响。
我还重定向,以便如果用户最终使用单词和生成器文件夹中的SSL版本的页面,则会将它们带到非ssl页面。
最后我有一个从页面的www版本重定向到非www版本,例如www.test.co.uk到test.co.uk
我的代码如下。
我想知道我是否以非常耗费资源的方式执行此操作,并且如果有更好的方法使用Web配置或类似的方式来执行此操作,或者是否如果它没有被破坏,请不要解决它?
我意识到我的代码非常基本且笨重,抱歉。
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
感谢@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>
答案 0 :(得分:0)
是的,如果使用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>