Regular expression pattern to match URL with or without http://www

Regular expression pattern to match URL with or without http://www

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Regular expression pattern to match URL with or without http://www

Matching URLs flexibly often trips up developers who rely on ad-hoc regex patterns. The requirement is specific: you need a single pattern that accepts http://www.example.com/etcetc, www.example.com/etcetc, and even the non-standard but common example.com/etcetc. This looks like three distinct cases, but with careful lookahead logic or optional grouping, you can collapse them into one robust expression.

The core challenge lies in handling the protocol prefix (http://), the optional www. subdomain, and the path component that follows. A naive approach might simply chain alternatives like (http://)?(www.)?, but this often fails to enforce strict boundaries or correctly handle the case where the protocol is present but www is omitted.

To address the specific source material's need, we must construct a pattern that handles the optional protocol and optional www prefix without creating ambiguity for the rest of the URL structure. Here is a regex that accomplishes this in PHP or similar engines:

(?:https?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

Let's break down the logic to ensure it meets your requirements. The pattern starts with (?:https?:\/\/)?. This non-capturing group makes the protocol optional. By using s? inside the brackets, we match either http: or https:. Crucially, the trailing \/\/ ensures that if a protocol is present, it must be followed by slashes; otherwise, the engine moves to the next part of the pattern.

Next comes (?:www\.)?. This handles the specific case from your examples where the host might start with www. or not. It is also optional here, allowing the regex to match example.com as well as www.example.com. Note that we do not make the protocol and the www mutually exclusive in a way that breaks validation; they are independent optional components sitting before the domain name.

The domain itself follows standard rules: [-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b. This matches the second-level domain and the top-level domain (like .com, .org). The word boundary \b after the TLD prevents matching partial domains like example.commail.com.

Finally, the path component ([-a-zA-Z0-9()@:%_\+.~#?&//=]*) captures everything that follows. This is where your examples like /etcetc are matched. The parentheses around this group make it optional as well, meaning the regex will match just the domain if no path is provided, covering edge cases gracefully.

Implementation in Laravel

If you are working within the Laravel ecosystem, applying this logic directly in a controller or route might feel clunky compared to leveraging built-in validation helpers. While you can pass this raw pattern to Rule::regex(), it is often cleaner to validate the URL format using Laravel's existing tools before applying strict regex matching.

However, if you specifically need to filter input strings that might lack the protocol for legacy reasons, here is how you apply it:

use Illuminate\Support\Facades\Validator;

$rules = [
    'url_input' => 'required|regex:/^(?:https?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/'
];

$validator = Validator::make($input, $rules);

if ($validator->fails()) {
    return redirect()->back()->withErrors($validator)->withInput();
}

Trade-offs and Pitfalls

One significant trade-off with this approach is the potential for false positives if the regex isn't anchored or if the character set for the path is too permissive. The example above anchors the pattern implicitly by requiring the end of the string (via $ at the end) or by ensuring the path component stops at a valid boundary.

Another pitfall is performance. Complex regexes with many optional groups can suffer from backtracking issues on very long inputs. If you are processing millions of requests, consider compiling the pattern once and reusing it, or using a more specialized library like League/Uri for robust URL parsing which handles edge cases like internationalized domain names (IDN) better than raw regex.

Finally, while this regex satisfies the specific request to match http://www.example.com, www.example.com, and example.com, remember that modern browsers often auto-complete URLs with protocols. If your application receives data from a form where users might paste these into an input field without typing the protocol, this pattern ensures you don't reject valid legacy links while still maintaining security by not accepting arbitrary text as a URL.

Our network