如何提高我的重定向代码的效率
How to improve efficiency of my redirect code
我在所有 ASP 页面的顶部都包含此内容,以便为所有不在名为 "words" 和 "generators" 的文件夹中的页面重定向到 https。我不希望他们使用 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
感谢@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>
是的,如果使用 URL 重写和在您的 web.config 中配置它。
规则如下所示:
<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>
我在所有 ASP 页面的顶部都包含此内容,以便为所有不在名为 "words" 和 "generators" 的文件夹中的页面重定向到 https。我不希望他们使用 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
感谢@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>
是的,如果使用 URL 重写和在您的 web.config 中配置它。
规则如下所示:
<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>