Prioritization of re-write rules in .htaccess and httpd.conf - apache

I'm confused on which file's rewriting rules gets hierarchy in an apache (xampp) server.
Do .htaccess rules get priority over httpd.conf rules?
If you can add the same rules to each, what is the difference between these two files?
What got me wondering about this stuff is because I'd like to update my favicon every second (for learning purposes) but the code below in my .htaccess file isn't working. My .htaccess file is in the server root folder along with my index.php file which is recognized when I go to http://localhost.
<IfModule mod_rewrite.c>
RewriteEngine On
ExpiresActive On
ExpiresByType image/x-icon "access plus 1 seconds"
</IfModule>
Any thoughts?

Related

htaccess: mod_expires.c except one or multiple folders

To the following browser caching via mod_expires.c in the .htaccess...
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 day"
</IfModule>
...I would like to add an exception: One or more folders shall not be cached. I tried a version with Directory before </IfModule>, but that led to a 500 Internal Server Error. That
<Directory "/absolute/path/to/specialfolder">
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
</Directory>
and that snippet
<Directory "/absolute/path/to/specialfolder">
ExpiresActive Off
</Directory>
What's wrong and what could help? (for one or a few folders)
The <Directory> directive is not permitted in a .htaccess context, it can only be used in a server (or virtualhost) context. (Hence the 500 error.)
The userland/.htaccess way would be to create another .htaccess file in that "specialfolder" with ExpiresActive Off in order to override the parent config.
Alternatively, you could perhaps use an <If> expression in the root .htaccess file.
For example:
# Turn off mod_expires for a specific directory
<If "%{REQUEST_URI} =~ m#^/specialfolder/#">
ExpiresActive Off
</If>
Where REQUEST_URI is the document-root-relative URL-path.
OR, only use mod_expires when not requesting these folder(s). For example:
# Only use mod_expires when NOT requesting the specific directories
<If "%{REQUEST_URI} !~ m#^/(specialfolder|anotherfolder)/#">
ExpiresActive On
ExpiresDefault "access plus 1 day"
</IfModule>
One or more folders shall not be cached.
Although disabling mod_expires does not necessarily prevent the resource from being cached. It simply prevents mod_expires from setting the Cache-Control (and Expires) headers. If you specifically want to disable caching then consider explicitly setting the relevant Cache-Control/Expires header(s) directly.
For example:
# Set env var when special "none-cache" folder requested:
SetEnvIf Request_URI "^/(specialfolder|anotherfolder)/" NOCACHE
# Set no-cache headers if NOCACHE env var is set
Header always set Cache-Control "no-store" env=NOCACHE
Header always set Expires "0" env=NOCACHE
In this example, you do not need to disable mod_expires since the Header directive will override mod_expires (since mod_headers is processed after mod_expires).

Simple page 301 redirects not working through .htaccess file

I'm trying to work out why my .htaccess file is not working for a simple page redirect, it's not picking it up. I started with just the bottom two lines but added extra (above) after reading a few posts, but still no luck.
AddType text/cache-manifest .manifest
Options +FollowSymLinks
RewriteEngine on
Redirect 301 /shoe-laces-boot-laces-shoestringuk.html https://www.shoestringuk.com/shoe-laces-boot-laces.html

.htaccess Rewrite and subdirectories not working

I have this as my .htaccess file:
AddType text/x-component .htc
ErrorDocument 404 /index.php
RewriteEngine on
RewriteRule ^maps/(\w+)/?$ /maps.php?lang=$1 [L]
RewriteRule ^(\w+)/?$ /index.php?lang=$1 [L]
The second RewriteRule is working PERFECTLY! No problems. But, the first one, is giving me a huge headache... It just doesn't work at all. It's redirecting to /maps.php, but it doesn't gives me the parameter, which is BASIC for me. It's like the GET params are not there, but are supposed to be there. I don't get it...because I've just copied a working RewriteRule, from StackOverflow, and addapted changing the word "search" to the word "maps".
Could anybody, please, help me with this thing? I'm pretty sure it'd be something simple, but I'm not seeing it...
EDIT: Apparently, there is a second .htaccess file in my server. "/logs/.htaccess".
Options +Indexes
RemoveHandler .html
RemoveType .html
AddType text/html .html
Satisfy any
Order Deny,Allow
Allow from MY_SERVER_IP
Deny from all
AuthType Basic
AuthName "Access to /logs"
There is no way I can delete this file. Or I didn't manage to do it. My hosting provider is 1&1.
I've found the problem through some deep searching after I realized it could be a problem with my hosting provider, 1and1. And I got it here 1and1 mod_rewrite issues
It was about an option you MUST add in your .htacces file which is:
Options -MultiViews
Now is working more than fine! Thanks to #kjetilh for the support.

Client side caching using Apache 2

I have a script that is consumming too much resource to provide data that could be retrieved only each minute. Is there a way to configure Apache 2 through an .htaccess file to specify headers telling to the client to keep the script result as is for one minute in its cache ?
I know this could be done through the script itself, but I would like to do this through the webserver's configuration.
mod_expires is the good solution.
If you are on a unix-like system :
a2enmod expires
apache2ctl restart
Then you will be able to define the expiration conditions for a given file, or define the cache policy according to mimetype through your .htaccess file.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif A3600
<Files scriptToCache.php>
ExpiresDefault A60
</Files>
</IfModule>
Here "A3600" means that the file expires 3600 seconds after access.
More information here : http://httpd.apache.org/docs/2.0/mod/mod_expires.html
Short answer is no - you need to return the caching headers from the script.
<IfModule mod_expires.c>
<FilesMatch "\.(jpe?g|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
</FilesMatch>
</IfModule>

Apache: apply rules to a URL before they're rewritten

I have a simple RewriteRule:
RewriteRule ^/r/[0-9]+/(.*)$ /$1
This is used for cache-busting. With every web site release I change the url prefix, e.g.:
/r/17/img/image.jpg gets /img/image.jpg.
I want to apply long expiry headers to these for example
<Directory /r>
Header unset ETag
FileETag None
ExpiresDefault "access plus 1 year"
</Directory>
Of course this doesn't work because after the RewriteRule is applied, the Directory doesn't match anymore.
How can I apply these rules inside the Directory directive to URLs accessed via /r/ ?
Thanks!
The <Directory> directive is for actual existing directories and not just URL paths. Try <LocationMatch> instead:
<LocationMatch "^/r(/|$)">
Header unset ETag
FileETag None
ExpiresDefault "access plus 1 year"
</LocationMatch>
Or change /r to your actual directory /img.