method

domain_from

rails latest stable - Class: ActionDispatch::Http::URL::DomainExtractor

Method not available on this version

This method is only available on newer versions. The first available version (v8.1.1) is shown here.

domain_from(host, tld_length)
public

Extracts the domain part from a host string, including the specified number of top-level domain components.

The domain includes the main domain name plus the TLD components. The tld_length parameter specifies how many components from the right should be considered part of the TLD.

Parameters

host

The host string to extract the domain from.

tld_length

The number of domain components that make up the TLD. For example, use 1 for “.com” or 2 for “.co.uk”.

Examples

# Standard TLD (tld_length = 1)
DomainExtractor.domain_from("www.example.com", 1)
# => "example.com"

# Country-code TLD (tld_length = 2)
DomainExtractor.domain_from("www.example.co.uk", 2)
# => "example.co.uk"

# Multiple subdomains
DomainExtractor.domain_from("api.staging.myapp.herokuapp.com", 1)
# => "herokuapp.com"

# Single component (returns the host itself)
DomainExtractor.domain_from("localhost", 1)
# => "localhost"