.htaccess - Set expire headers while url rewriting - .htaccess

I'm rewriting using htaccess like:
# ENABLE REWRITE
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^gallery/(.+)$ /image.php?image=$1 [L,QSA,NC]
# EXPIRES CACHING
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
As you can see, I'm also setting expire period using .htaccess. Expire works if I remove line RewriteRule ^gallery/(.+)$ /image.php?image=$1 [L,QSA,NC], but then obviously I will have problem with rewriting. How can I combine this two things to play well together?
I'm on LiteSpeed server.

You can try this FilesMatch directive for the rewritten PHP file:
<FilesMatch "image\.php$">
ExpiresDefault "access plus 1 year"
</FilesMatch>

Related

How to prevent .htaccess holding codeigniter session data

I have website built on Codeigniter and on product page which has loads of images etc so I setup htaccess caching as following:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_deflate.c>
However issue is when customer go through the check out process and place order I am using codeigniter sess_destroy to destroy use session. However when I go back to product URL it shows there is items in cart. When I click to check out again. I have nothing in my cart which means codeigniter did destroy the session.
How to resolve this? Please help

How to fix a 3xx redirect using .htaccess file?

I want to redirect from my domain name to a custom index page and am using this rewrite rule for it
# HTID:1827272: DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES BELOW
php_flag display_errors off
php_flag log_errors off
# DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES ABOVE HTID:1827272:
IndexIgnore *
Options -Indexes
#Alternate default index pages
ErrorDocument 404 /error/theme/404.html
ErrorDocument 500 /error500.php
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 4 month"
# CSS
ExpiresByType text/css "access plus 5 month"
# Data interchange
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rdf+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/ld+json "access plus 0 seconds"
ExpiresByType application/schema+json "access plus 0 seconds"
ExpiresByType application/vnd.geo+json "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"
# Favicon (cannot be renamed!) and cursor images
ExpiresByType image/vnd.microsoft.icon "access plus 1 month"
ExpiresByType image/x-icon "access plus 5 year"
# HTML
ExpiresByType text/html "access plus 0 seconds"
# JavaScript
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType application/x-javascript "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
# Manifest files
ExpiresByType application/manifest+json "access plus 1 week"
ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Media files
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/bmp "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType image/webp "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# Web fonts
# Embedded OpenType (EOT)
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType font/eot "access plus 1 year"
# OpenType
ExpiresByType font/opentype "access plus 1 year"
# TrueType
ExpiresByType application/x-font-ttf "access plus 1 year"
# Web Open Font Format (WOFF) 1.0
ExpiresByType application/font-woff "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
# Web Open Font Format (WOFF) 2.0
ExpiresByType application/font-woff2 "access plus 1 year"
# Other
ExpiresByType text/x-cross-domain-policy "access plus 1 week"
AddType application/x-httpd-php .xml
</IfModule>
# to prevent ifame the code bellow
<IfModule mod_headers.c>
Header always append X-Frame-Options SAMEORIGIN
Header set X-Content-Type-Options nosniff
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^gamehup.000webhostapp.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.gamehup.000webhostapp.com [NC]
RewriteRule ^(.*)$ https://gamehup.com/browser-games/index [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteBase /
#only for the root directory
RewriteCond %{REQUEST_URI} ^/$
#if the uri is not already index.php
RewriteCond %{REQUEST_URI} !^/browser-games/index [NC]
RewriteRule ^$ /browser-games/index [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.gamehup\.com [NC]
RewriteRule ^(.*)$ https://gamehup.com/browser-games/index [R=301,NC,L]
The only problem is am using Cloudflare to always use https that why it redirects first from
http://gamehup.com to https://gamehup.com then to http://gamehup.com/browser-games/index then to https://gamehup.com/browser-games/index
And that lots of redirects, how can I make it only one?
from http://gamehup.com to https://gamehup.com/browser-games/index
This is all the htaccess code am using.

.htaccess Leverage browser caching not working

I have following code in .htaccess file and it is not working for me :
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType images/jpg "access 1 year"
ExpiresByType images/jpeg "access 1 year"
ExpiresByType images/gif "access 1 year"
ExpiresByType images/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType images/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
<ifModule mod_deflate.c>
<filesMatch "\.(js|css|html|php)$">
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>
Please suggest me what i am doing wrong here ?

.htaccess subfolder not working getting error

I m getting .htaccess subfolder error
I have a subfolder localhost/folder/subfolder/
my site under folder is running excellent but when i try to open subfolder 404 page opens
RewriteEngine On
RewriteCond %{index} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

CakePHP and .htaccess Asset Caching

I am still pretty new to CakePHP and am having trouble figuring out how to optimize asset caching.
Back when I still coded in pure PHP, this is what I would do with my .htaccess and header.inc.php files:
.htaccess:
<IfModule mod_rewrite.c>
# Turn the rewrite engine on
RewriteEngine On
# The following rewrite rule makes it so that if a URL such as
# http://example.com/css/style.1291314030.css is requested
# then it will actually load the following URL instead (if it exists):
#
# http://example.com/css/style.css
#
# This is to increase the efficiency of caching. See http://bit.ly/9ZMVL for
# more information.
RewriteCond %{DOCUMENT_ROOT}/$1/$2.$3 -f
RewriteRule ^(css|js)/(.*)\.[0-9]+\.(.*)$ /$1/$2.$3 [L]
</IfModule>
<IfModule mod_expires.c>
# Optimize caching - see http://yhoo.it/ahEkX9 for more information.
ExpiresActive On
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType application/x-javascript "access plus 1 year"
</IfModule>
header.inc.php:
foreach ($css_to_use as $current_css)
{
echo "\n\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"css/$current_css." . filemtime("{$_SERVER['DOCUMENT_ROOT']}/css/$current_css.css") . ".css\">";
}
This setup worked quite well because when I worked on client websites, I never had to tell the client to perform a hard refresh or clear their cache; it was totally automatic and still had the benefits of caching.
I see that in CakePHP's "app/config/core.php" file, one can use this line of code:
Configure::write('Asset.timestamp', 'force');
However, that only makes the URLs look like this:
<link rel="stylesheet" type="text/css" href="/css/style.css?1291314030" />
So it doesn't work the way I'd like it to. What is the best way to achieve asset caching?
Thanks!
Appending a query string is effectively the same as changing the url, browsers will consider it different and reload the asset, be it CSS, images or anything else.
Step 1: Change your webroot .htacess to this
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##
Step 2: sudo a2enmod expires
Step 3: sudo service apache2 restart
Step 4: Drink a beer, life is good.

Resources