htaccess rewrite rule jpg or jpeg
To target both .jpg and .jpeg extensions in .htaccess, use the regex pattern jpe?g. This matches “jpg” and “jpeg” (where the ‘?’ makes the ‘e’ optional). Common use cases include blocking hotlinking, setting cache headers, or rewriting URLs, often combined as \.(jpe?g|png|gif)$ to cover multiple image types.
Common .htaccess Rules for JPG/JPEG
1. Block Hotlinking (Protect Bandwidth)
This code prevents other websites from directly linking to your JPEG images. Naukri.com
apache
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpe?g|jpeg|png|gif)$ - [F]
2. Browser Caching for Images
Set expiration headers to make images load faster for repeat visitors.
apache
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
</IfModule>
3. Case-Insensitive Matching
Using [NC] (No Case) ensures it matches .JPG, .Jpeg, .jpeg, etc.. Joomla! Documentation +1
apache
RewriteRule \.(jpe?g)$ - [NC]
Key Considerations
- Syntax:
jpe?g= ‘j’, ‘p’, optional ‘e’, ‘g’. - Server Type: These rules work on Apache and LiteSpeed servers.
- Implementation: Add these rules to your root
.htaccessfile.Joomla! Documentation +4
One Comment