htaccess ExpiresDefault conditional - .htaccess

I have in my Apache 2.4's .htaccess the following:
ExpiresActive On
ExpiresDefault "access plus 1 month"
However, pages that start with today_ (files actually not present on the server) should not be cached. How would you write that condition?

If you are using Apache 2.4 and you should be :) You can simply use a condition statement. I think these are overlooked a lot. I would approach it this way.
#if URL contains today_ turn it off
<If "%{REQUEST_URI} =~ /today_/">
ExpiresActive Off
</If>
<Else>
ExpiresActive On
ExpiresDefault "access plus 1 month"
</Else>

Related

Getting 500 Internal Server Error instead of 404 Not Found Error

So when I was testing my website which is hosted on Bluehost, I wanted to test out my 404.html which was going well. However, I then noticed that I keep getting the default 500 internal server error from Bluehost when I typed something like this, https://www.example.com/folder/webpage/awdawdasd (basically putting a slash after the webpage name and typing random things). In my htaccess, I made it so that it allows users to visit the website without the .html at the back, i.e. hides the extension in the URL. How can I fix it so that when the user type random things after the html page like shown in the example?
Note that I am using Bluehost's file manager only, i.e. hosting my own html pages on Bluehost that I have coded as part of my assignment, I am not using Wordpress or anything.
Here is my htaccess code,
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
# Prevent user from going to the website's index
Options -Indexes
</IfModule>
## EXPIRES HEADER CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
# Images
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
# Video
ExpiresByType video/webm "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/mpeg "access plus 1 year"
# Fonts
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/otf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
# CSS, JavaScript
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
# Others
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
</IfModule>
## REWRITE RULES
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirects user from http to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
# Allow browsers to access webpages without .html at the end
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
# Redirect user to webpage url without .html if they typed .html in the url
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
# Removes .php extension
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
# Redirect user to webpage url without .php if they typed .php in the url
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
# Redirect users to the error webpages if user receives a 404 error, etc.
ErrorDocument 401 /errorpages/401
ErrorDocument 403 /errorpages/403
ErrorDocument 404 /errorpages/404
ErrorDocument 500 /errorpages/500
ErrorDocument 502 /errorpages/502
ErrorDocument 503 /errorpages/503
# End of Apache Rewrite Rules
</IfModule>
# php -- END cPanel-generated handler, do not edit
Bluehost 500 Internal Server Error Message Image
Edit: I have contacted Bluehost and was able to deduce that it was not from their side but the problem lies in the code above. I have also edited the apache rewrite code to redirect users if they type in .html at the back.
Update: Managed to find the error logs and it says
/home2/otakuabr/public_html/.htaccess: RewriteRule: bad flag delimiters

htaccess config to improve increase OpenCart 2.3.0.2 pagespeed and redirect to https://www

How can I config my htaccess file? I need to increase page speed and redirect to https://www. version of website.
Opencart 2.3.0.2, php 7.3
SSL already installed and turned on.
Images already compressed.
Config:
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip
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 2100 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"
# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Your document html
ExpiresByType text/html "access plus 0 seconds"
# Data
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
# RSS feed
ExpiresByType application/rss+xml "access plus 1 hour"
# Favicon (cannot be renamed)
ExpiresByType image/x-icon "access plus 1 week"
# Media: images, video, audio
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 video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# HTC files (css3pie)
ExpiresByType text/x-component "access plus 1 month"
# Webfonts
ExpiresByType font/truetype "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# CSS and JavaScript
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
# 1.To use URL Alias you need to be running apache with mod_rewrite enabled.
# 2. In your opencart directory rename htaccess.txt to .htaccess.
# For any support issues please visit: http://www.opencart.com
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
I found this in the documentation:
Additional Settings that may need to be enabled for some servers
Uncomment the commands by removing the # sign in front of it. If you
get an "Internal Server Error 500" after enabling any of the following
settings, restore the # as this means your host doesn't allow that.
If your cart only allows you to add one item at a time, it is possible register_globals is on. This may work to disable it:
php_flag register_globals off
If your cart has magic quotes enabled, This may work to disable it: php_flag magic_quotes_gpc Off
Set max upload file size. Most hosts will limit this and not allow it to be overridden but you can try php_value upload_max_filesize
999M
set max post size. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all
fields php_value post_max_size 999M
set max time script can take. uncomment this line if you have a lot of product options or are getting errors where forms are not
saving all fields php_value max_execution_time 200
set max time for input to be recieved. Uncomment this line if you have a lot of product options or are getting errors where forms are
not saving all fields php_value max_input_time 200
disable open_basedir limitations php_admin_value open_basedir none
What can I do to make my site more secure?
Redirect to https://www
You can copy below code and paste you htaccess file
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip
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 2100 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"
# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Your document html
ExpiresByType text/html "access plus 0 seconds"
# Data
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
# RSS feed
ExpiresByType application/rss+xml "access plus 1 hour"
# Favicon (cannot be renamed)
ExpiresByType image/x-icon "access plus 1 week"
# Media: images, video, audio
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 video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# HTC files (css3pie)
ExpiresByType text/x-component "access plus 1 month"
# Webfonts
ExpiresByType font/truetype "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# CSS and JavaScript
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
# 1.To use URL Alias you need to be running apache with mod_rewrite enabled.
# 2. In your opencart directory rename htaccess.txt to .htaccess.
# For any support issues please visit: http://www.opencart.com
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
# https:// to https://www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
Increase Website Speed / Performance
I will suggest you use this really good extension for the boost your OpenCart website. I always used it and its reviews also appreciate it.
Download the extension here

Unwanted Rewrite in .htaccess

I've got an app on the backend of my site that is having issues being accessed. The site itself uses .htaccess for routing purposes, but I seem to have my rules incorrect or something. I'm basically trying to route everything that does not contain the directory tools/ or ajax/ to index.php:
DirectoryIndex index.php
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
# the folders mentioned here will be accessible and not rewritten
RewriteCond %{THE_REQUEST} !/(ajax|tools)/
# but rewrite everything else
RewriteRule ^ index.php [L]
# ----------------------------------------------------------------------
# UTF-8 encoding
# ----------------------------------------------------------------------
# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8
# Force UTF-8 for a number of file formats
AddCharset utf-8 .atom .css .js .json .rss .vtt .xml
# ----------------------------------------------------------------------
# A little more security
# ----------------------------------------------------------------------
# "-Indexes" will have Apache block users from browsing folders without a
# default document Usually you should leave this activated, because you
# shouldn't allow everybody to surf through every folder on your server (which
# includes rather private places like CMS system folders).
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
# Block access to "hidden" directories or files whose names begin with a
# period. This includes directories used by version control systems such as
# Subversion or Git.
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
# Block access to backup and source files. These files may be left by some
# text/html editors and pose a great security danger, when anyone can access
# them.
<FilesMatch "(\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
# prevent access to PHP error log
<Files php_errors.log>
Order allow,deny
Deny from all
Satisfy All
</Files>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 6 hours"
</IfModule>
However, when attempting to run the long- executing scripts in the tools directory, it eventually gives me a fatal error saying the path ./controllers/tools.php cannot be found, meaning that the URI has been processed by the routing system when it shouldn't have been. Any thoughts?
EDIT - updated with the full .htaccess.
Have it this way:
Options -MultiViews
RewriteEngine on
RewriteBase /
# the folders mentioned here will be accessible and not rewritten
RewriteCond %{THE_REQUEST} !/(ajax|tools)/ [NC]
# but rewrite everything else
RewriteRule ^ index.php [L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1

How to do .htaccess redirect and HTTP cache for static images in a no-cookie subdomain

We want to do redirects for our site photos from domain.com/images/ to a no-cookie img.domain.com.
Currently, we have images directory in public_html, URLs are:
http://domain.com/images/subfolder-1/title-of-the-image.jpg
http://domain.com/images/subfolder-2/title-of-the-image.jpeg
http://domain.com/images/subfolder-3/title-of-the-image.gif
http://domain.com/images/subfolder-4/title-of-the-image.png
In cPanel, img.domain.com has been set to publi_html/images
We tried to unset cookie and redirect via htaccss. Yet, it does not work.
In htaccess,
AddDefaultCharset UTF-8
## Re-directing
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?images\. [NC]
RewriteRule ^(.*)$ http://www.img.domain.com/$1 [L,NE,R=301]
# Expire headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 29030400 seconds"
ExpiresByType image/x-icon "access plus 29030400 seconds"
ExpiresByType image/jpeg "access plus 29030400 seconds"
ExpiresByType image/jpg "access plus 29030400 seconds"
ExpiresByType image/png "access plus 29030400 seconds"
ExpiresByType image/gif "access plus 29030400 seconds"
</IfModule>
# Headers
<IfModule mod_headers.c>
<FilesMatch "\\.(ico|jpe?g|png|gif|swf)$">
Header unset Cookie
Header unset Set-Cookie
Header set Cache-Control "max-age=29030400, public"
Header set Last-Modified "Mon, 24 Mar 2014 00:00:00 GMT"
</FilesMatch>
</IfModule>
We wonder what to keep/add/change in the above .htaccess code, which is located inside images directory, or if there are better ways. Thanks!
Add this rule just below RewriteEngine On in /images/.htaccess file:
RewriteCond %{HTTP_HOST} !^(www\.)?images\. [NC]
RewriteRule ^(.*)$ http://www.images.domain.com/$1 [L,NE,R=301]

.htaccess Redirect 301 - Nothing Happens

I want to 301 redirect a couple old URLs
http://www.mysite.com/contact/old-page.php
http://www.mysite.com/contact/another-old-page.php
to this one
http://www.mysite.com/contact/
I'm pretty sure my code is correct, since I have used this on other sites with no problem. I know redirecting is enabled because the non-www to www redirect works just fine. But for some reason this one-off redirect doesn't work; nothing happens at all when I visit the old page, as if I didn't do anything.
Here's the complete .htaccess file:
#Prevent viewing of htaccess
<Files .htaccess>
order allow,deny
deny from all
</Files>
#Enable symbolic links
Options +FollowSymLinks
RewriteEngine On
#Force www version of URL
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
#This is the bit that isn't working
Redirect 301 /contact/old-page.php http://www.mysite.com/contact/
Redirect 301 /contact/another-old-page.php http://www.mysite.com/contact/
#Gzip
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/php text/plain text/xml text/css text/javascript application/javascript application/x-javascript
</ifmodule>
#Caching
<ifmodule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType text/javascript "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType image/x-icon "access 1 month"
</ifmodule>
Your redirects look fine, but one possible conflict is that you're using mod_alias (Redirect) and mod_rewrite (RewriteRule) together and the URI passes through both modules and both modules can process them, not independently of each other. And sometimes this causes unexpected results.
Try just using rewrite rules and remove the Redirect statements. Try adding these two redirect rules before your "force www" rule:
RewriteRule ^contact/old-page\.php$ http://www.mysite.com/contact/ [L,R=301]
RewriteRule ^contact/another-old-page\.php$ http://www.mysite.com/contact/ [L,R=301]

Resources