As @dan mentioned in comments, this is a regular expression ("regex" for short and often referred to as a "pattern" in Apache docs). Specifically, Apache uses the PCRE (Perl Compatible Regular Expressions) flavour of regex.
The first argument to the
RewriteRule directive is a regex that matches against the URL-path component of the requested URL. In
.htaccess this URL-path does not include the slash prefix. So, given a URL of the form
https://example.com/foo/bar.html, the URL-path that this regex matches against is
This regex matches everything. The
^ at the start of the regex is a start-of-string anchor (it matches from the start of the URL-path). It also captures (
(.*)) the entire URL-path in a backreference (so it can be used later).
As regex go, this is relatively simple. However, as per your question, this can be "simplified". In this particular regex, the
^ anchor is superfluous since the
.* part is greedy and consumes the entire string by default. There is also no need for the capturing backreference (the parentheses surrounding
(.*)), since this is not being used later in the substitution (2nd argument) part of the directive.
So, this regex can be immediately simplified to:
However, since the goal of this regex is to simply process every URL, it doesn't need to explicitly match anything. So, it can be further optimised. Instead of explicitly matching everything, as it does with
.* (the regex parser traversing the entire URL-path) you can use an alternative regex, such as:
^ - A single start-of-string anchor effectively matches everything (since the assertion is always successful), but it doesn't actually "match" anything. You could also use the end-of-string anchor instead, ie.
$ - however, it is less common to see this used in this way.
.? - This matches an optional single character. The
. matches any character (except newline) and the trailing
? makes the preceding pattern optional. In a per-directory context (such as
.htaccess), this single character needs to be optional, because the URL-path could be empty (in the case of the document root). (In a server context, you would remove the
? to leave a single dot.)
Whichever you use is really a matter of preference.
Aside: In your example, you should ideally include a trailing slash on the target URL. Something has to. If you omit it then you are relying on the browser appending the trailing slash to the hostname (which it will do) before issuing the redirected request.
So, in summary:
RewriteRule ^ https://www.example.com/ [R=301,L]
Regex are incredibly powerful. But, naturally, with that "power" comes additional complexity.