<Directory ~ /home/user/(site|sitedev)>
...
</Directory>
<DirectoryMatch ^/home/user/(site|sitedev)>
...
</DirectoryMatch>
"/home/user/(site|sitedev)"
/home/user/(site|sitedev)
/home/my/site
/home/my/siteblog
/home/my/sitestore
or any other that starts with /home/my/site doesn't go deeper like /home/my/siteblog/random
Can these two be a solution and what is the difference?
^/home/my/site.*
/home/my/^site.*
I'm not a specialist but I would say:
<Directory ~ "^/home/user/(site|sitedev)">
is equivalent to
<DirectoryMatch "^/home/user/(site|sitedev)">
Both are core built-in. I prefer the second one as it's much
clearer to read for a beginner.
You are right, quotes are required in case of spaces.
I personally always quote them, for consistency in the config file.
^/home/my/site.*
is the correct one:
^
means "beginning with". This is why it can't be put before
"sites" like you mentioned in /home/my/^site.*
. If you don't
put this ^
at the beginning, then an URL such as
/test/home/user/sitedev
would also match. This is because a
pattern is used like a search and if it's found in the path then
it matches, and you'll enter the <DirectoryMatch>
block.In opposite of ^
, you can use $
, meaning "finishing with".
This is useful to match all JPEG files with \.jpe?g$
, where
the dot should be escaped because .
means "any character" and
?
means that the "e" char before is optional. Of course, this example would not be used on a <DirectoryMatch>
but on a
<FilesMatch>
.
Caution, the regex can be case-sensitive or not, so it could not match
/img/IMG234.JPG
depending on the config. I think this depends on
RegexDefaultOptions.
If ICASE
is "on" or not.
But I usually prefer solving this config question by enabling
the "case-insensitive" flag at the beginning of the pattern with
(?i)\.jpe?g$
or enabling it for a specific part by wrapping it
between (?i:
and )
like this: (?i:\.jpe?g)$
.
Examples:
(?i)\.jpe?g$
(?i:\.jpe?g)$
/img/([^/]+)(?i:\.jpe?g)$
()
is used to capture a part of the path. Here it's the file
basename. It will be available in a variable $1
(1 = first group).
If you have another capturing group, then you will have a second
variable, called $2
. See this example with 2 capturing groups.[^ ]
means "not a char in the list". So [^/]
means "any char
which isn't a slash". The *
means 0 or N times. But +
means
1 or N times. So [^/]+
will match the basename of the file.