问题原文
原帖链接 · Vlastimil Burián · CC BY-SA 4.0
I have just copy and pasted a rewrite rule for a redirect from some website. It contains:
^(.*)
I want to know what it means. Why is it so complicated? Can it be simplified?
The whole .htaccess file currently contains:
# turn on the rewrite engine
RewriteEngine On
# unconditional redirect to the new web
RewriteRule ^(.*) https://www.example.com [R=301,L]
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
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 foo/bar.html.
^(.*)
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. It just needs to be successful. 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 is successful for everything (since the assertion is always successful), but it doesn't actually "match" anything (so is more efficient). 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, since in a server context there is always at least one character in the URL-path; the slash prefix.)
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 (fixing) 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.
Further reading:
- Apache mod_rewrite Introduction - Includes basic regex
- https://www.regular-expressions.info/ - great resource for digging deeper into regex.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
