Redirect to a single site

Use this ASP code to redirect your pages to a single site. Search engines may penalize you for duplicate content if they find content at yoursite.com and www.yoursite.com or multiple domains pointing at the same site.

‘—————————————–
If  lcase(Request.ServerVariables(“HTTP_HOST”))=”lyonscom.com” OR lcase(Request.ServerVariables(“HTTP_HOST”))=”www.sitenumber2.com” OR lcase(Request.ServerVariables(“HTTP_HOST”))=”sitenumber2.com” Then
 Response.Status = “301 Moved Permanently”
 If lCase(Request.ServerVariables(“URL”)) = “/default.asp” Then   ‘ change home page here
  If Len(Request.Querystring)=0 Then
   Response.AddHeader “Location”, “http://www.lyonscom.com/
  Else
   Response.AddHeader “Location”, “http://www.lyonscom.com/?” & Request.Querystring
  End If
 Else
 If Len(Request.Querystring)=0 Then
   Response.AddHeader “Location”, “http://www.lyonscom.com” & Request.ServerVariables(“URL”)
  Else
   Response.AddHeader “Location”, “http://www.lyonscom.com” & Request.ServerVariables(“URL”) & “?” & Request.Querystring
  End If
 End If
 Response.End
End If
‘—————————————–

Note, the Response.Status line will tell the search engine crawler to remove the duplicate content and only index the conent at the new location.

“Save As” Dialog Box for Streaming File

To download files using a “Save As” dialog box try this code:

        Select Case FileExt
            Case “gif” ContentType = “image/gif”
            Case “tif” ContentType = “image/tiff”
            Case “jpg” ContentType = “image/jpeg”
            Case “pdf” ContentType = “application/pdf”
            Case “avi” ContentType = “video/x-msvideo”
            Case “mp3” ContentType = “audio/mpeg”
            Case “mpg” ContentType = “video/mpeg”
            Case “wav” ContentType = “audio/wav”
            Case “rar” ContentType = “application/x-rar-compressed”
            Case “zip” ContentType = “application/x-zip-compressed”
            Case “exe” ContentType = “application/x-msdownload”
            Case Else
             ‘ FilePath = “”
             ContentType = “application/x-msdownload”
        End Select

        Response.Clear
        Response.Buffer = False
        Dim adoStream
        Set adoStream = Server.CreateObject(“ADODB.Stream”)
        adoStream.Open()
        adoStream.Type = 1
        adoStream.LoadFromFile(FilePath + “/” + FileName)
        Response.AddHeader “Content-Disposition”, “attachment;filename=” & Right(FileName,Len(FileName)-InStr(FileName,”/”))
        Response.ContentType = ContentType
        Response.BinaryWrite(adoStream.Read())
        adoStream.Close
        Set adoStream = Nothing
        Response.End

The ASP buffering limit is about 4 megs. Files larger than that will give an error like this:

   “Response Buffer to exceed its configured limit”

To stream out files larger than 4 Meg you can increase the “AspBufferingLimit” value. Edit the MetaBase.xml file in the C:\WINDOWS\system32\inetsrv folder. The default value is 4194304. Check the “Enable Direct Metabase Edits” check box before editing this file and you will not need to stop and start the WWW service.

SelectedValue which is invalid because it does not exist in the list of items

To populate drop down lists I bind the list to a lookup table in the database (i.e. States). Occasional the parent table (i.e. Customers) has a value that is “not on the list” and this error is genreated:

    ‘ddlState’ has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

The only thing I’ve been able to figure out to do is add the parent table value to the drop down list. This only works when the value saved in the parent table is the actual value and not the foreign key (the ID of the child table such as StateID). Here is my code:

<asp:DropDownList ID=”ddlState” runat=”server” DataSourceID=”SqlDataSource1″ DataTextField=”StateName” DataValueField=”StateAbbr”></asp:DropDownList> <asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:DATABASE_01 %> SelectCommand=”SELECT [StateAbbr], [StateName] FROM [States] ORDER BY [StateName]”></asp:SqlDataSource>

Then in code:

Sub Page_Load()

ddlState.AppendDataBoundItems = True
Dim Items As New ListItem
Items.Text =
“Select One”
Items.Value = “”
ddlState.Items.Add(Items)

If Not IsPostback Then

… ‘ read data from database

… ‘  load values into controls

Items = New ListItem
Items.Text =
“” & rdUser(“State”).ToString
Items.Value =
“” & rdUser(“State”).ToString
ddlState.Items.Add(Items)
ddlState.SelectedValue =
“” & rdUser(“State”).ToString

 

I’ve tried putting a ‘Try/Catch’ around setting the ddlState.SelectedValue but that is not where the error occurs. The error occurs as the page is being rendered. When using the SqlDataSource control is used the data gets bound as the page is being rendered. The page load event occurs before the page is rendered so the drop down selected value is set before the data is bound! This method prevents the “does not exist” error messages all the time. If you have a better solution I’d sure like to hear it!

 

Force SSL ON or OFF Function

When working on a site that may have some pages in SSL (the check out pages) and most pages not I use a function to switch between SSL and Non SSL pages.

First, I set the non secure and SSL URL’s in the config file under appSettings:

    <add key=”SSLURL” value=”https://www.mydomain.com”/>
    <add key=”NonSSLURL” value=”http://www.mydomain.com”/>

Next, in my utility class I add this function:

    Public Function PageStartUp()
        If Request.ServerVariables(“HTTPS”).ToLower = “on” And Not gIsSSLRequired Then
            If Request.QueryString.ToString.Length > 0 Then
                Response.Redirect(ConfigurationManager.AppSettings(“NonSSLURL”) & Request.ServerVariables(“SCRIPT_NAME”) & “?” & Request.QueryString.ToString)
            Else
                Response.Redirect(ConfigurationManager.AppSettings(“NonSSLURL”) & Request.ServerVariables(“SCRIPT_NAME”))
            End If
        End If
        If Request.ServerVariables(“HTTPS”).ToLower = “off” And gIsSSLRequired And ConfigurationManager.AppSettings(“SSLURL”).ToLower.Substring(0,5)=”https” Then
            If Request.QueryString.ToString.Length > 0 Then
                Response.Redirect(ConfigurationManager.AppSettings(“SSLURL”) & Request.ServerVariables(“SCRIPT_NAME”) & “?” & Request.QueryString.ToString)
            Else
                Response.Redirect(ConfigurationManager.AppSettings(“SSLURL”) & Request.ServerVariables(“SCRIPT_NAME”))
            End If
        End If
        Return True
    End Function

Be sure to add the public variable gIsSSLRequired in the class as well.

Then in the page load event of each page I add the following:

gIsSSLRequired = True
Call PageStartUp()

Cool, huh?