2015-10-29 41 views
0

我有这个包括在我的所有ASP页面的顶部,重定向到HTTPS的所有页面不在称为“文字”和“发电机”的文件夹。我不希望他们使用SSL,因为我听说转移到SSL可能会对AdSense收入产生重大影响。如何提高我的重定向代码的效率

我也重定向,这样如果用户在单词和生成器文件夹中的页面的SSL版本上结束,它会将它们带到非ssl页面。

最后我有一个从www版本的页面重定向到非www版本,例如www.test.co.uk to 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阿吉拉尔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

是的,这可以很容易地完成,可能要快得多(不再在脚本中,不再解析代码,本机代码中的正则表达式和逻辑,加上更易维护等),如果使用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> 
+0

感谢@Carlos阿吉拉尔母马! – 4532066