PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: htaccess
htaccess - phponwebsites.com
Showing posts with label htaccess. Show all posts

17 Nov 2013

Compressing resource with gzip using .htaccess

                       After you host your site, check the speed of your site in google insights page speed. You may be get less speed for your site if your information is not compressed. Mostly all sever can compress the files before send it to download. If the server didn't do this, you have to do compress your file. It can be done by .htaccess.

Enable compression using mod_deflate() in .htaccess:

                     Gzip is a server process that compress your files on fly before transmission to the user. It is used to compress text files like html, css and javascript. It reduces the visitor time and your bandwidth. Due to compression, the speed of your site will be increased.
Example:
                <IfModule mod_deflate.c>
                      AddOutputFilterByType DEFLATE text/text
                      AddOutputFilterByType DEFLATE text/plain
                      AddOutputFilterByType DEFLATE text/html
                      AddOutputFilterByType DEFLATE text/css
                      AddOutputFilterByType DEFLATE text/xml
                      AddOutputFilterByType DEFLATE application/x-javascript
                      AddOutputFilterByType DEFLATE application/javascript
                      AddOutputFilterByType DEFLATE text/javascript
                </IfModule>
Explanation:
            Where,
                         mod_deflate.c provides the output filter ' DEFLATE ' which is used to compress the file in your browser before send it to download.

Enable compression using mod_gzip() in .htaccess:

                 
     
                <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 also done by your php file. Just add this to your header file.
<?php
           if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')) ob_start('ob_gzhandler');
           else
           ob_start();
?>
After add this content to your .htaccess file, you can check whether your site is compressed or not in  website.

Related Post:

15 Nov 2013

Restrict users to access pages from site using .htaccess

                      Someone may be misuse your information. So .htaccess can be used to restrict unwanted user for accessing your page. It can be done by number of ways, something within that.

Authentication  to your site:

                  In this method, first you have to create user name and password for user who are access your pages. File is saved with name .htpasswd. You've to store your .htpasswd file in .htpasswds folder. You can store it anywhere except in public_html.
  Syntax:
               AuthName "Anything your wish to display on dialog box"             
               AuthType Basic
               AuthUserFile /home/username/.htpasswds/.htpasswd
               Require valid-user
Now anybody try to access your cpanel, the web browser ask username and password.
      where,
                 AuthName     - what you wish to display message on dialog  box to user, when they get to access this page.
                 AuthType Basic - AuthType selects the method that is used to authenticate the user who are try to access particular page on your site. The ' Basic ' method is implemented by mod_auth_basic which sends the password from user to server unencrypted.
                 AuthType Digest is another method supported by the apache server which is implemented by mod_auth_digest.
                 AuthUserFile - where your .htpasswd file located.
                 Require valid-user - tells the server to authentication needed to access this page
You can provide authentication for particular file.
  Syntax:
               AuthUserFile /home/username/.htpasswds/.htpasswd
               AuthType Basic
               AuthName "Anything your wish to display on dialog box"             
               <Files "filename to provide authenticate">
               Require valid-user   
               </Files>
  In password protection method, you can disable it.
  Syntax:
              Require valid-user
              Allow from 127.0.0.1
              Satisfy Any

Deny users by IP Address:

              Some users may be violate your contents. So you have to protect that particular user only. It can be done by .htaccess.
  Syntax:
               order allow, deny
               deny from 192.168.1.1
               allow from all
    where,
              the browser block the users from IP 192.168.1.1
 Considered another example,
  Syntax:
               order allow, deny
               deny from 192.168.1.
               allow from all
 where, the browser block the users from IP address starts with 192.168.1.

You can allow only one user and block all users using it.
  Syntax:
               order allow, deny
               allow from 192.168.1.1
               deny from all
 where, the browser allow the user from IP address 192.168.1.1 only

Deny users by referrer:

               Considered as a example, you host a new site. you may be got referrals. /at this stage, your site have been spammed. This spam will affect the log file. It render your log files useless. We can solve it by .htaccess.
  Syntax:
              RewriteEngine On
              # Options +FollowSymlinks
              RewriteCond %(HTTP_REFERRER) example\.com [NC]
              RewriteRule .* - [F]
   where,
             RewriteEngine On - turn on the mod_rewrite.c
             Options +FollowSymlinks should be start with ' # '. Otherwise it give error message ' 500 Internal Server Error ' .
             RewriteCond %(HTTP_REFERRER) example\.com - it tells server to block traffic from example.com
             [NC] - not case sensitive

You can create multiple referrer.
  Syntax:
              RewriteEngine On
              # Options +FollowSymlinks
              RewriteCond %(HTTP_REFERRER) example\.com [NC, OR]
              RewriteCond %(HTTP_REFERRER) anotherone\.com 
              RewriteRule .* - [F]
  where,
            it says server to block traffic from example.com and anotherone.com.

14 Nov 2013

What is .htaccess

                       Hypertext  access is shortly called as htaccess. It is a directory level configuration file. Directory level means, where you locate your .htaccess file, it configure that directory only. Mostly, on server, it is placed on public_html folder for configure the files to access. You can control your site using it. It is supported by web servers.
   

Uses of .htaccess:


why we use .htaccess

           - DirectoryIndex uses
           - used for restrict the user for access pages from website
           - redirect the urls
           - gives direction to server rather than search engine
           - create error document
           - compressing resources with gzip
           - add expires headers
           - enable and disable the additional functionality of apache webserver
           - prevent access to php.ini file
           - force scripts to display as a source code
           - ensure media files are downloaded instead of played
     

How to start with .htaccess?


   Syntax:
         <IfModule mod_rewrite.c>
             RewriteEngine On
             RewriteCond  %(REQUEST_FILENAME) !-f
             RewriteCond  %(REQUEST_FILENAME) !-d
             RewriteCond  %(REQUEST_FILENAME) !-l
             RewriteBase /
             DirectoryIndex index.php
             RewriteRule ^home index.php [L]
         </IfModule>
  Explanation:
     <IfModule mod_rewrite.c>
                                IfModule test whether mod_rewrite.c is included in apache. If mod_rewrite.c
is included, then it run. Otherwise it doesn't get run.
     RewriteEngine On
                                It tells apache to turn on its RewriteRules.
     RewriteCond
                                RewriteCond execute the next rewrite rules if it is correct.
%(REQUEST_FILENAME) is a variable based on url you request
!-f, !-d, !-l are extension added to regular expression.
where,
         !- f - rewrite condition if it is not  file
         !-d - rewrite condition if it is not directory
         !-l  - rewrite condition if it is not link
 if a file, taken from variable %(REQUEST_FILENAME) does not exist on the file system, then return true.
        RewriteBase /
                              It provide base for RewriteRules.                  
        DirectoryIndex index.php
                              It tells apache to which file is run first when server access to particular directory.
        RewriteRule
                              It redirect the urls.
RewriteRule ^home index.php [L]
      it means, when url get ' home ' on address bar, it runs the file index.php. Using this method, we can change our address bar contents to anything.
          For example,
                 RewriteRule ^sample.html sample.php [L]
                 where,
                            you type sample.html in your address bar, the .htaccess runs sample.php file.
  we can pass the values in address bar using it.
     
Related Post: