As far as I know, they are all the same.
But I wonder why people use the rex
in the RewriteRule
Pattern.
Does ^
have any performance benefits?
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]
Does /
have any performance benefits?
RewriteRule /(.*) https://%{SERVER_NAME}/$1 [R,L]
Does ()
have any benefits?
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
Thanks.
2
Answers
Yes, the regex will only apply to the begining of the url so it will be faster
Not really, it just matches a slash character
In regex the parenthesis create a group to capture substrings
There are few more things to keep in mind here (in addition to the other answer):
This rule, as it is (alone, without any other rewrites or conditions), should probably be written as
In this case, the rewrite engine will not be involved which might result in
(probably negligible) performance benefit.
If you have a reason to use
%{SERVER_NAME}
, keep in mind that%{SERVER_NAME}
might have different value than you expect, if UseCanonicalName is set to a non-default value.%{HTTP_HOST}
should be safer.Even if you have to use
RewriteRules
(orRewriteCond
), there is no need to use regex to capture full request URI, it is already available in%{REQUEST_URI}
, so this should be faster:Keep in mind that RewriteRule behaves differently when use used in
.htaccess
compared to.conf
:Or to put it more clearly: in
.htaccess
, there is no leading/
.( just a reminder: you should not use.htaccess
at all if you have access to Apache configuration files)