Enable browser caching for RewriteRule - .htaccess

I wrote these lines in .htaccess to enable browser caching for images:
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
And it works. But also I need redirect requests for non-exist images to php file. I added these lines to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^resized/w([0-9]+|auto)/h([0-9]+|auto)/(.*) resize.php?w=$1&h=$2&src=$3
And redirect works good. But it disabled browser caching.
How I can enable browser caching for this rule?

Related

How to allow pdf files in .htaccess?

I have this following codes in my htaccess file, but it doesn't allow pdf file to open, it will redirect to 404 page. how can I allow pdf files to open based on my following htaccess file.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|public|assets|uploads|themes|updates|asset|mob|robots\.txt\.pdf)
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
<FilesMatch "\.(jpg|jpeg|png|gif|swf|pdf|css)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
I highly appreciate your help. Thank

How do you rewrite http to https for an addon domain in htaccess

I have a cPanel hosting account at Godaddy with an SSL for an addon domain. I need to rewrite http to https. The problem is that all methods of rewrite are loading from the webroot, not the folder containing the index or even the htaccess that has the rewrite rule. I know there are topics on this already on this forum but I have tried those suggested solution, that's how I have what you see in the code quote, and they did not work. Please do not arbitrarily close this thread.
webroot (loading this content / not desired)
|
target_directory (htaccess & SSL in question)
The following is the complete htaccess at the time of this post
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
#RewriteRule (.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule (.*)$ https://emeraldcoast\.rocks/$1 [R,L]
RewriteRule ^([a-zA-Z0-9\-_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9\-_]+)/$ index.php?page=$1
<filesMatch ".(xml|txt|html|js|css)$">
ExpiresDefault A7200
Header append Cache-Control "max-age=290304000, public"
</filesMatch>
<filesMatch ".(xml|txt|html|php)$">
AddDefaultCharset utf-8
</filesMatch>
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
Using a control panel especially cpanel takes control of the server and configures things a lot differently than a standard setup. Anyway, you might need add a rewritebase to your addon domain. Inside .htaccess for the addon domain change the rewritebase to your addon folder.
RewriteBase /target_directory

.htaccess: Image Redirect

I was shown this rewrite for getting images to redirect to a particular web page. And it allows for the usage of images on my site and others.
RewriteCond %{HTTP_REFERER} ^$
RewriteCond %{REQUEST_URI} ^/uploads/(\d+)-(.+)\.(png|gif|jpe?g|bmp)$ [NC]
RewriteRule (.*) http://www.sitename.com/view/%1-%2/
This works great if I try to directly access an image from the address bar and I'm currently viewing my site. The problem I am noticing is if a user writes out a link on another site:
Click
It will take them to that image, not to the page.
What modifications should I be making to the rewrite so that it will redirect the user to the /view/ page.
EDIT: Another thing I have noticed is when posting an image to Facebook, using the above rewrite, it will not display the thumbnail as it once did. I am assuming this is something that cannot be worked around?
RewriteCond %{HTTP_REFERER} ^$ [OR]
RewriteCond %{HTTP_REFERER} !^http://domain.com
RewriteCond %{REQUEST_URI} ^/uploads/(\d+)-(.+)\.(png|gif|jpe?g|bmp)$ [NC]
RewriteRule (.*) http://domain.com/view/%1-%2/
For prevent caching:
<filesMatch "\.(png|jpeg|jpg|gif)$">
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</filesMatch>

rewrite condition in .htaccess

I am trying to make partners readable on the website i.e. http://domain.com/partners. but the pinnaclecart doesn't allow it to be readable or accessable on the browser. i created the directory partners so i could write the whole different files from the pinnaclecart.
inside .htaccess: you can see "partners" at the end.
EDIT II
RewriteEngine ON
RewriteRule ^homepage.html$ index.php
RewriteRule ^home.html$ index.php
<IfModule mod_headers.c>
Header unset ETag
FileETag None
<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2020 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^((.)?)$ index.php?p=home [L]
RewriteCond $1 /var/www
RewriteRule ^(.+)$ / [L]
RewriteCond $1 !^(\#(.)*|\?(.)*|admin\.php(.)*|ses\/(.)*|ecc\/(.)*|index\.php(.)*|login\.php(.)*|\.htaccess(.)*|images\/(.)*|\.htaccess\.back(.)*|3cc4da-pinnacle_zend_3\.7\.7_sdk\.zip(.)*|dump\.sql(.)*|content\/(.)*|download\.php(.)*|readme\.txt(.)*|in_case_of_install\/(.)*|robots\.txt(.)*|images\.zip(.)*|partners\/(.)*)
RewriteRule ^(.+)$ index.php?url=$1&%{QUERY_STRING} [L]
</IfModule>
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|ico|flv|jpg|jpeg|png|gif)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
Why is it not accessable on the browser?
EDIT
is there no answer to this for solutions?
As people mentioned you may wish to post more of your .htaccess file to help debugging, but I couldn't help but notice that the last part of your regex reads:
partners\/(.)
This seems odd as it will only match single letter items in that directory. So I am not 100% sure its your problem but I suspect you intended that to read:
partners\/(.)*

htaccess 301 redirect not working

I'm trying to add a simple 301 rule to the .htaccess file of a codeigniter site.
redirect 301 /newsletter http://sub.domain.com/newsletters/may2010
When I visit http://sub.domain.com/newsletter the redirect goes to
http://sub.domain.com/newsletters/may2010/?/newsletter
I'm not sure where the ?/newsletter is coming from. Full .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
redirect 301 /newsletter http://sub.domain.com/newsletters/may2010
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
#####################################################
# CONFIGURE media caching
#
Header unset ETag
FileETag None
<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
#
#####################################################
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
How can I fix this?
Heh, this is Apache pulling a sneaky order-of-processing trick on you. As it turns out, the fact that you put your Rewrite command at the top of the file doesn't mean that it's what's actually executed first. What happens instead is that mod_rewrite is run first, followed by mod_alias (which is responsible for handling your Rewrite).
This results in the following transformation, per mod_rewrite:
newsletter --> index.php?/newsletter
mod_rewrite happily sets the query string to ?/newsletter, but because you don't have the PT flag specified, does not passthrough the rewritten URL index.php to mod_alias. Therefore, mod_alias still sees the /newsleter path, redirects it to http://sub.domain.com/newsletters/may2010, and appends the (now changed) query string ?/newsletter to the end to seal the deal. Fun stuff, right?
Anyway, a quick-fix for this scenario would be to ignore requests for just newsletter:
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !newsletter$
RewriteRule ^(.*)$ index.php?/$1 [L]

Resources