htaccess Tricks

Welcome to Perishable Press! This article, Stupid htaccess Tricks, covers just about every htaccess “trick” in the book, and is easily the site’s most popular offering. In addition to this htaccess article, you may also want to explore the rapidly expanding htaccess tag archive. Along with all things htaccess, Perishable Press also focuses on(X)HTML, CSS, PHP, JavaScript, security, and just about every other aspect of web design, blogging, and online success. If these topics are of interest to you, I encourage you to subscribe to Perishable Press for a periodic dose of online enlightenment 😉

We got this blog from www.perishablepress.com for blog more information Click Here

 

Enable gzip compression

Paste the below code in your .htaccess file :

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

You can check it using online tools like : checkgzipcompression.comvarvy.com/tools/gzip

Read more

How do I remove index.php from my URLs?

To remove index.php from the URL, and to redirect the visitor to the non-index.php version of the page

RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index.php [NC]
RewriteRule (.*?)index.php/*(.*) /$1$2 [R=301,NE,L]
This will cleanly redirect /index.php/myblog to simply /myblog.

Using a 301 redirect will preserve Google search engine rankings.

Code to remove index.html from URL

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /index.html HTTP [NC]
RewriteRule (.*)index.html$ /$1 [R=301,L]

301 redirect using web.config file

The first example will redirect single pages to a new location. For example, important pages of your site have .htm extensions and you want the new location to be its own directory (IE. http://domain.com/services.htm will change to http://domain.com/services/).

  1. Open web.config in the directory where the old pages reside
  2. Then add code for the old location path and new destination as follows:
<configuration>
<location path="services.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://domain.com/services" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="products.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://domain.com/products" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>

Preventing firewall bypass by .htaccess

If someone knows your hidden Hosting IP address, they can bypass our firewall and try to access it directly. It is not common or easy to do so, but for additional extra security, we recommend only allowing HTTP access from our firewall.

//If using Apache, you can add the following to the top of your .htaccess file:
<FilesMatch ".*">
    Order deny,allow
    Deny from all
    Allow from 192.88.134.2
    Allow from 192.88.135.2
    Allow from 185.93.228.2
    Allow from 185.93.229.2
    Allow from 185.93.230.2
    Allow from 192.88.134.0/23
    Allow from 185.93.228.0/22
    Allow from 192.124.249.0/24
    Allow from 199.223.236.179
    Allow from 146.148.117.213
    Allow from 23.251.134.134
    Allow from 178.33.238.180
    Allow from 142.4.217.0/24
    Allow from 167.114.0.0/24
    Allow from 192.99.17.0/24
    Allow from 5.196.79.0/24
    Allow from 130.211.0.0/16
    Allow from 104.155.0.0/16
</FilesMatch>
//If using Nginx, you can add the following to the configuration file of your virtual host:

location / {
    allow 192.88.134.2;
    allow 192.88.135.2;
    allow 185.93.228.2;
    allow 185.93.229.2;
    allow 185.93.230.2;
    allow 192.88.134.0/23;
    allow 185.93.228.0/22;
    allow 192.124.249.0/24;
    allow 199.223.236.179;
    allow 146.148.117.213;
    allow 23.251.134.134;
    allow 178.33.238.180;
    allow 142.4.217.0/24;
    allow 167.114.0.0/24;
    allow 192.99.17.0/24;
    allow 5.196.79.0/24;
    allow 130.211.0.0/16;
    allow 104.155.0.0/16;
    deny all;
    # Existing NGINX rules
}