B4J Question www in URL with secure site

Discussion in 'B4J Questions' started by aaronk, Jul 31, 2019.

  1. aaronk

    aaronk Well-Known Member Licensed User

    Hi,

    I have my B4J server running a web server.

    I have followed the tutorial:
    https://www.b4x.com/android/forum/threads/server-building-web-servers-with-b4j.37172/#content

    I have also enabled a SSL certificate on it and working fine.

    If the customer opens the website like https://mysite.com it loads the site fine. If they open http://mysite.com it converts to https://mysite.com and this is also working fine.

    So the above works fine.

    However, if they open www.mysite.com it comes up saying the site is not secure..

    Is there a way in making it if the user type www.mysite.com to redirect it to https://mysite.com (so it removes www. part) ?
     
  2. jimmyF

    jimmyF Active Member Licensed User

    I believe that you will need to add "www.mysite.com" to your certificate.

    Or, have a separate site with an automatic redirect to your secure site.

    -j
     
  3. Jorge M A

    Jorge M A Active Member Licensed User

  4. MarkusR

    MarkusR Well-Known Member Licensed User

    maybe you can setup some rules in the .htaccess file.
     
    Jorge M A likes this.
  5. aaronk

    aaronk Well-Known Member Licensed User

    Yeah I have the https filter already.

    I have made a small change to the script and seems to be working fine from the tests I have done, and not 100% if it's the best way in doing this but thought I would share in case someone else wants to know how I managed to get it to work:

    Code:
    Public Sub Filter(req As ServletRequest, resp As ServletResponseAs Boolean
        
    If req.Secure Then
            
    If req.FullRequestURI.StartsWith("www."Or req.FullRequestURI.StartsWith("https://www."Then
                
    Dim url As String = req.FullRequestURI
                resp.SendRedirect(url.Replace(
    "https://www.""https://"))
                
    Return False
            
    End If
           
         
    Return True
       
    Else
            
    If req.FullRequestURI.StartsWith("www."Or req.FullRequestURI.StartsWith("http://www."Then
                
    Dim url As String = req.FullRequestURI
                url = url.Replace(
    "http://www.","http://")
                url = url.Replace(
    "www.","")
                resp.SendRedirect(url.Replace(
    "http:""https:") _
                   .Replace(Main.srvr_public.Port, Main.srvr_public.SslPort))
                
    Return False
            
    End If
           
           
         resp.SendRedirect(req.FullRequestURI.Replace(
    "http:""https:") _
           .Replace(Main.srvr_public.Port, Main.srvr_public.SslPort))
         
    Return False
       
    End If
    End Sub
    Based on the above, if someone goes to http://www.mysite.com or https://www.mysite.com or www.mysite.com it will change it to https://mysite.com
     
    Jorge M A, OliverA and universengo like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice