Match file in directory but not sub-directories - regex

I am running Apache httpd on Windows. I want to get Apache to make index.html uncacheable - but only the home index.html, no other index.html files. This is what I have so far:
<Directory "D:\path\to\root">
<FilesMatch "index.html$">
Header set Cache-Control "max-age=0, must-revalidate"
</FilesMatch>
</Directory>
It works - but works for all index.html files. How can I narrow this down to just the one index.html? Apparently I can use regex in Directory, but I this did not work:
<Directory ~ "D:\\path\\to\\root">

You can specify a path instead of just a file name inside the Files directive. So I guess the same applies to the FilesMatch directive. That way you could exclude files that carry a path from their subsirectory when being matched against the regular expression.
Note that I have not tested this, but it might be worth a try:
<Directory "D:\path\to\root">
<FilesMatch "^[^\\]*index.html$">
Header set Cache-Control "max-age=0, must-revalidate"
</FilesMatch >
</Directory>
Note that I am also not sure about which backslash (\) you have to escape and which not. This strange path notation MS-Windows uses internally really is a problem. Not only when dealing with regular expressions :-)

This works nicely:
<Location /index.html>
Header set Cache-Control "max-age=0, must-revalidate"
</Location>

Related

FilesMatch specify path

<FilesMatch "\.html$">
How do I specify only .html files in base directory?
I want
https://www.example.com/page.html
But not https://www.example.com/directory/page.html
Since the rule is in .htaccess of the root directory, as became clear from the discussion below, try this
<FilesMatch "^\.(html|php)$">
<If "%{REQUEST_URI} =~ m#^/[^/]+\.(html|php)$#">
(your directives)
</If>
</FilesMatch>
See https://httpd.apache.org/docs/2.4/sections.html#file-and-web, https://httpd.apache.org/docs/2.4/mod/core.html#if and https://httpd.apache.org/docs/2.4/expr.html.

Filesmatch is not working in .htaccess file

I have created a .htaccess file and trying to have a CAS login when the user goes to a url (ie: https://www.mycompany.com/users/forms/immigration.php)
I have created a filesmatch if the page begins with either imm or imp and ends in a .php, it should prompt a CAS login. I have used the FilesMatch tag and here the following code:
<FilesMatch "^(imm|imp)\.php$">
Authtype CAS
Require valid-user
</FilesMatch>
Is there a different directive tag I should use?
You will need to include .* after initial part to allow matching immigration.php:
<FilesMatch "^(imm|imp).*\.php$">
Authtype CAS
Require valid-user
</FilesMatch>

AliasMatch everything except home page to "content" folder

I am putting all of my website's content (aside from the home page) into a "content" folder at the root of the site. This is Apache 2.4.25.
I want http://www.example.com to serve the DirectoryIndex (i.e. index.html) at C:/DocumentRoot/. The following works fine for that.
<Directory "C:/DocumentRoot/website">
Options None
AllowOverride None
Require all granted
</Directory>
I then want to have http://www.example.com/anything1/anything2 serve the DirectoryIndex at C:/DocumentRoot/content/anything1/anything2. After adding the following, accessing http://www.example.com gives a Forbidden error, though the AliasMatch works.
AliasMatch "^/(.+)$" "C:/DocumentRoot/website/content/$1"
<Directory "C:/DocumentRoot/website/content/">
Require all granted
</Directory>
Any idea what's happening or have a better/working alternative?
In this case, mod_rewrite is a little easier to read and later extend then a negative assertion regex in the AliasMatch.
RewriteEngine ON
RewriteCond %{REQUEST_URI} !^/content
RewriteRule ^/(.+) "C:/DocumentRoot/website/content/$1"
A change to the AliasMatch so it ignores the DirectoryIndex (index.html) allows it to work as expected.
AliasMatch "^/(?!index.html)(.+)$" "C:/DocumentRoot/website/content/$1"
(?!index.html) ensures that the implicit index.html is not matched. The (.+) picks up anything else and pulls it from the content folder.

Regular Expressions help for htaccess

I am interested in excluding some documents from being indexed on search engines, so I am using X-Robots-Tag to achieve it. However, I need to have a regular expression which will select documents within a specific direcotory i.e. secret document in my case.
For Example
/dir1/dir2/secret documents/file1.pdf
/dira/dir1/dir2/secret documents/file2.pdf
/dir1a/secret documents/file3.pdf
/dir1a/other documents/file4.pdf
As you can see, there could be any number of directories on left, but the last directory if is "secret documents", i want to disallow it using following code in htaccess.
Regular Expression
RegEx should fit in below
<Files ~ "\.pdf$">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
You can use:
<Files ~ "secret\ documents/.+?\.pdf$">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
<Files ~ "secret\ documents\/.+?\.pdf$">
Header set X-Robots-Tag "noindex, nofollow"
</Files>
Don't forget to escape the forward slash after documents.

How can I escape slash sign `/` in Apache `<If>` directive's regex?

I wanted to serve .xhtml files as
application/xhtml+xml if the browser says that it accepts it.
text/html otherwise
I tried doing it with mod_rewrite but it didn't work with Options -FollowSymLinks (see Why I do I get 403 Forbidden when viewing files affected by Apache RewriteRule if I have `Options -FollowSymLinks`?).
Then, I tried
<Files "*.xhtml">
<If "%{HTTP:Accept} !~ /application\/xhtml\+xml/">
ForceType text/html
</If>
</Files>
But I get a syntax error: Failed to compile regular expression.
Meanwhile, I use this code...
<Files "*.xhtml">
<If "%{HTTP:Accept} !~ /xhtml\+xml/">
ForceType text/html
</If>
</Files>
... which works, but I want to match the correct MIME type.
You could use an escape code like \x2F instead of the /.
It looks like improving this is still under construction as of Apache 2.4. Apache team member "covener" recommends m#regexp# instead.
So your code would look like this...
<If "%{HTTP:Accept} !~ m#application/xhtml\+xml#">