Skip to content

Instantly share code, notes, and snippets.

@redesigned
Last active December 19, 2023 11:44
Show Gist options
  • Save redesigned/6259000 to your computer and use it in GitHub Desktop.
Save redesigned/6259000 to your computer and use it in GitHub Desktop.
Helpful Mod Rewrites and .htaccess rules
#### IMAGE PROTECTION ####
#protect images from hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domainname.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|bmp|tiff|svg)$ - [NC,F,L]
#protect images from hotlinking send new image
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domainname.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|bmp|tiff|svg)$ http://www.domainname.com/donthotlink.png [NC,R,L]
#protect images from hotlinking but allow blank referrers and send new image
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domainname.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|bmp|tiff|svg)$ http://www.domainname.com/donthotlink.png [NC,R,L]
#### AUDIO PROTECTION ####
#protect audio from hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domainname.com [NC]
RewriteRule \.(mp3|oog|wav)$ - [NC,F,L]
#### VIDEO PROTECTION ####
#protect video from hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domainname.com [NC]
RewriteRule \.(mp4|mpg|avi|flv|mpeg|mov|wmv|mts)$ - [NC,F,L]
#### FONT PROTECTION ####
#protect fonts from hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domainname.com [NC]
RewriteRule \.(eot|woff|otf|ttf|ttc|svg)$ - [NC,F,L]
#### FONT ALLOW CROSS DOMAIN ####
#Allow Cross Domain Fonts
<FilesMatch "\.(ttf|ttc|otf|eot|woff|svg)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
#### ERROR DOCUMENTS ####
#Set Custom Error Document Locations
ErrorDocument 401 /error/401.html
ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 500 /error/500.html
ErrorDocument 501 /error/501.html
#### CUSTOM REWRITES ####
#rewrite from a subdirectory back to root, helpful for moving magento or wordpress out of subdirectories.
RewriteRule ^anydirectory/(.*)$ $1
#rewrite from an old domain to a new domain keeping the page or arguments
RewriteCond %{HTTP_HOST} ^old.domain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^olddomain2.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
#rewrite from www to www-less keeping the page or arguments
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
#rewrite from www-less to www keeping the page or arguments
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
#### MAINTENANCE-PAGE REDIRECT ####
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.html [R=302,L]
</IfModule>
@chaithrasn
Copy link

how to add this....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment