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

27 Nov 2013

How to redirect to www using .htaccess

                       Type your domain name without www in address bar. Check whether it redirects to your site or give a 301 error message. If you got 301 error message, then you will solve this problem using .htaccess.

Syntax:
              RewriteEngine On
              RewriteCond %(HTTP_HOST) ! ^www.domainname.com$ [NC]
              RewriteRule ^(.*)$ http://www.domainname.com/$1 [L, R=301]

Explanation:
              Where,
                          .htaccess force the all http requests to use either www.domainname.com or domainname.com .
                           HTTP_HOST - represents your domain name.
                           RewriteCond   - check the conditions if you typed domain name is not start with www, it 
redirect to next rewrite rule. 
                           ! ^www.domainname.com$ - means you typed domain name is not start with www.
                           NC - represents not case sensitive.
                           RewriteRule - means it redirect domain name without www to http://www.doaminname.com.
                           ^ - starting
                           .* - one or more characters.
                           $ - ending
                           L - if rule matches, don't process any other rewrite rules below this one.

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: