What is .htaccess? [ force ssl using htaccess ]

.htaccess is a configuration file for use on web servers running the Apache Web Server software.

htaccess
htaccess

1.Where can you find the .htaccess file?

.htaccess file will be stored inside your website directory.If you cant see .htaccess file in your cpanel. On top of your cpanel account inside file manager on top right corner you can find settings, enable show hidden files, save it. Refresh the page to see the file.You can edit the file accordinly.

2.How to force SSL using htaccess file ?

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Simple paste the code in your .htaccess file and that it , your site will show a padlock and no more displaying insecure site.

3.How to upgrade or set the php version using .htaccess file ?

If you want your wordpress/website to run the php version to 5.5 , simply paste the following code to your file

 <FilesMatch "\.(php4|php5|php7|php3|php2|php|phtml)$">
 SetHandler application/x-lsphp55
 </FilesMatch>

If you want your wordpress/website to run the php version to 7.0 , simply paste the following code to your file

 <FilesMatch "\.(php4|php5|php7|php3|php2|php|phtml)$">
 SetHandler application/x-lsphp70
 </FilesMatch>

4.How can you enable GZIP compression using .htaccess file?

<ifmodule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</ifmodule>

5.Enable browsing cache via .htaccess file

You can change the month or period of time,till when the cache should expire or refresh it by itself

# BEGIN Expire headers  
<IfModule mod_expires.c>  
  # Turn on the module.
  ExpiresActive on
  # Set the default expiry times.
  ExpiresDefault "access plus 2 days"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/svg+xml "access 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
  ExpiresByType image/ico "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"
  ExpiresByType text/html "access plus 600 seconds"
</IfModule>  
# END Expire headers 

6.How to disable cache via htaccess file?

Simply paste the code to your htaccess file.

#Disables GZIP
SetEnv no-gzip 1
    
#Turns off the expires headers for Apache
<IfModule mod_expires.c>
  ExpiresActive Off
</IfModule>

# Disable Caching
<IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
</IfModule>
How To Force SSL Using Htaccess

Leave a Reply

Your email address will not be published. Required fields are marked *

Drive More Customers Through Digital