.htaccess caching with FilesMatch - regex

I need to make browser caching with htaccess file.
From this question I found out how to add extensions to htaccess file.
<FilesMatch "\.(js|jpeg|jpg)$">
But I need to add extensions. But exclude some of the files.
I found something like this from this question
<FilesMatch ^((myfile|myfile2)\.js$|myphoto\.jpe?g)$>
Add all js and jpeg files except "myfile.js", "myfile2.js", "myphoto.jpg"
How can I do this?
Thank you

Try this
<FilesMatch "((?<!myfile|myfile2)\.js|(?<!myphoto).jpe?g)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
This will match all js and jpeg files except myfile.js, myfile2.js, and myphoto.jpeg using negative lookahead/lookbehind. Kind of ugly but I couldn't find a nice way to do this.
You can then have a separate files match for only those files and set a different header:
<FilesMatch "((myfile|myfile2)\.js|myphoto\.jpe?g)$">
Header set Cache-Control "max-age=3600, public"
</FilesMatch>

Related

Why does this regex misfire on css?

I'd like any files not matching my extension list to be forced to download and so I added
<FilesMatch "(?i)(?<!\.css|\.eot|\.flv|\.gif|\.html|\.ico|\.jpeg|\.jpg|\.js|\.json|\.m4v|\.mov|\.mp3|\.ogg|\.pdf|\.png|\.svg|\.ttf|\.txt|\.webm|\.webp|\.woff|\.xhtml|\.xml)$">
Header set Content-Disposition attachment
</FilesMatch>
to .htaccess but I am getting the header on CSS files as well. Running https://regex101.com/r/M26y55/4 says "Your regular expression does not match the subject string."

Including multiple .txt files into httpd.conf using regex?

I want to be able to include multiple virtualhost files into httpd.conf. I know that it's possible to include one using something like:
include virtualhost-1.txt
However, I want to be able to import all virtualhost files within a certain directory, without needing to hardcode the include statements. Is there an appropriate command/syntax for this? I know the directory and have a suitable regular expression:
^.+-[0-9]+\.txt
The syntax of the Include directive is the following:
Include file-path|directory-path|wildcard
You can do for example:
Include /usr/local/apache2/conf/vhosts/dom1-*.conf
to include all configuration starting with dom1- from the directory /usr/local/apache2/conf/vhosts/. Note that you can use wildcards but you cannnot use a full regex.

htaccess match specific file types in root directory

I would like to match specific file types within the root directory the website. This htaccess file also sits in the root directly.
I've got this so far:
<FilesMatch "\.(php|png|ico)$">
Allow from all
</FilesMatch>
None of my attempts to restrict the matches to the same directory have been successful. I'm sure it's something super simple.
Try to do it this way:
<FilesMatch ".*\.(php|png|ico)$">
Allow from all
</FilesMatch>
I found out that only looks at the file name. It does not look at the directory part of the filename at all.
There is a but your not able to use it in a htacces file.
So the only way is through a Rewrite Rule. Something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule "^(subdir/).*/.*\.(html|json)$" "-" [PT,L]
# -- Deny Everything Not Matched Above -- #
RewriteRule "/*" "-" [F]
</IfModlue>
The above will only allow html or json files to be accessed in a second (or greater) sub-directory within a directory called subdir in the same folder as the htaccess file. Everything else will be forbidden. Assuming mod_rewrite is installed. The files will also be treated as files, which is not the default behavior for a rewrite rule.

How do I make .htaccess such that going to the folder will show the contents of the folder?

How do I make .htaccess such that going to the folder in the web browser, where .htacess resides, will show the contents of the folder? Maye this is not something achieved by .htacess file but has something to do with changing the permission of the folder("directory").
I do not want to use an index.html file at all because the contents of the directory (folder) will change and then the index.html will be invalid.
I have a follow-up question. After this is done, is there then a way to programmatically use C# code to get the contents of the directory listing?
So you're trying to access your folders directory via the web browser:
All you need to add is:
<IfModule mod_autoindex.c>
IndexOptions FancyIndexing IconHeight=16 IconWidth=16
</ifModule>
You will get something that looks a bit like this:
You can of course play around with the settings and add extra things like:
NameWidth=25
DescriptionWidth=40
IconsAreLinks
This is not necessary though, it really depends if this is just for you, or public access too. Does it need to be pretty :P
You can add an .htaccess file inside that specific folder then add this at the top of the .htaccess file. Remove any index.html file that you have there also.
Options +Indexes

I Need Help On Implementing mod_gzip

I'm working on a website that showed in the page source a suggestion to implement gzip compression to help my pages display faster. I have downloaded the zip file for mod_gzip 1.23.26.1a (latest version). I'm running Apache 2.4.14.
The link below is the installation instructions from the zip file. When you look at the section Static integration of mod_gzip it says in step 4 to read file INSTALL. The file is missing so I cannot use them to install mod_gzip.
http://schroepl.net/projekte/mod_gzip/install.htm
Note: When I open the index.hm file from the folder there is no link to the left that displays the install link above. I don't remember how I found that link. There are three makefile files but I have no idea what to do with them because the link gives no details.
I did find this link http://nadeausoftware.com/node/33 where I looked at the Mac steps in the section Enable file compression using mod_gzip. I guess these are valid instructions to implement mod_gzip. I don't know how to perform step 4: Compile the module using the included instructions. The documentation provided in the zip file does not have any compiling instructions that I could find. All I have are .c and .h files in the folder. The few places that I could find instructions were using files that end in .cpp. I don't know what I'm supposed to compile.
I would like to use mod_gzip to help speed up my websites but I don't know where else to find information on how to implement it.
I continued to search but could not find any answers to help me. During my search I found out that mod_deflate was available in apache2. Since there is an old debate between mod_gzip and mod_deflate with no consensus I decided to implement mod_deflate. I added the following statements to my apache config file which will compress all file types except images, files already compresses and pdf files. I have reviewed the log file and it's working well.
# HTTP compression using mod_deflate
LoadModule deflate_module ${MODULE_INSTALL_PATH_PREFIX}/usr/libexec/apache2/mod_deflate.so
<IfModule mod_deflate.c>
# compress all files types except the following
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|jpg|tif?|bmp|svg)$ \ no-gzip dont-vary
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|bz2|sit|rar)$ \ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog /var/log/apache2/deflate_log deflate
</IfModule>