Forum Discussion

smoraneng's avatar
smoraneng
Icon for Nimbostratus rankNimbostratus
Jun 03, 2019

Redirect based on # of domain parts

I have a server that has several websites on it. I need to create a redirect for the sites, but it needs to be rather generic because the developers don't always tell us what the sites are. I want to create a rule that will redirect based on the number of domain parts, for example,

www.site.com or blog.site.com would be left alone, as each of these contains 3 domain parts, however, if the url entered were site.com, I would redirect to www.site.com. Any help would be appreciated.

  • Try this

    when HTTP_REQUEST {
    	if { !(string tolower [HTTP::host] starts_with "www") && (llength [split [HTTP::host] "."] == 2) } {
    		HTTP::redirect "http://www.[HTTP::host][HTTP::uri]"
    	}
    ]

    KR,

    Dario.

  • Try this

    when HTTP_REQUEST {
    	if { !(string tolower [HTTP::host] starts_with "www") && (llength [split [HTTP::host] "."] == 2) } {
    		HTTP::redirect "http://www.[HTTP::host][HTTP::uri]"
    	}
    ]

    KR,

    Dario.

  • Try this.. if the host header is comprised of only two elements such as 'site.com'

    It will prepend www. and redirect

    when HTTP_REQUEST {
        set hostParts [split [HTTP::host] .]
        if {[llength $hostParts] == 2} {
            HTTP::redirect https://www.[HTTP::host][HTTP::uri]
        }
    }