B4J Question www in URL with secure site

aaronk

Well-Known Member
Licensed User
Longtime 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) ?
 

jimmyF

Active Member
Licensed User
Longtime 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
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
maybe you can setup some rules in the .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime 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:

B4X:
Public Sub Filter(req As ServletRequest, resp As ServletResponse) As 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
 
Upvote 0
Top