PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: November 2013

29 Nov 2013

How to access pages through one page in site using .htaccess

                        You can access all pages from your site through a one page. It can be possible in .htaccess. In this type, you have to specify the other page urls on that one page.

               RewriteRule ^(.*)$ index.php?url=$1 [L]

                    Where, when you try to access any pages in your site, at first your index.php file run. Then it
navigate to where you fixed it to go, it goes to that page. In your index.php should be like this:

                 if($url[0]=='home.php') {

                        where, you try to access your home.php file, first it comes to index.php and then check whether the home.php page is present or not. It navigates to home.php, if it is present in your site.

You can also hide all your files except home page:

               RewriteRule ^(.*)$ index.php

                  where, whatever you type in your address bar, you should be navigates to your index file.

Related Post:

28 Nov 2013

How to enable short tags in php using .htaccess

                       Mostly you would use ' <?php echo $name; ?> ' tag for display anything you want. You can also display this type of single variable value by <?=$name?>. But sometime your browser doesn't take this tag. On that time, you won't get proper output. So you can be done this by .htaccess.

              php_value short_open_tag 1
                      Now you can use short tag in your site.

It can also done by php.ini file. You've to change in php.ini file. In your php.ini file, find " short_open_tag=off", then change 'off ' to 'on'. Save the changes and restart your WAMP server.

Related Post:

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.

              RewriteEngine On
              RewriteCond %(HTTP_HOST) ! ^$ [NC]
              RewriteRule ^(.*)$$1 [L, R=301]

                          .htaccess force the all http requests to use either or .
                           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. 
                           ! ^$ - means you typed domain name is not start with www.
                           NC - represents not case sensitive.
                           RewriteRule - means it redirect domain name without www to
                           ^ - starting
                           .* - one or more characters.
                           $ - ending
                           L - if rule matches, don't process any other rewrite rules below this one.

26 Nov 2013

How to redirect the urls using .htaccess

                        You can redirect the urls from one file name to another. It is possible in .htaccess. It can be done by 'RewriteRule' in .htaccess. The 'RewriteRule' is used to redirect the urls from one path to another. You can make your site urls very simple using RewriteRule method.

Rewrite urls using .htaccess:

                       You can rewrite the urls using .htaccess. When your address bar gets one page, the .htaccess can run another page. The .htaccess rewrite the urls from one page to another. Consider the following example.
                RewriteRule ^ex1.php example.php [L]

                First Rule:
                    when the ex1.php file is going to run, .htaccess file runs the example.php instead of ex1.php.

Hide file extension like php, html using .htaccess:

                             You can hide extensions like .php and .html using .htaccess. Consider the following  example.
                RewriteRule ^home home.php [L]

                    When the browser get home to run, .htaccess runs the home.php instead of home. In this method, you can hide your file extensions like .html, .php .

Remove query string using .htaccess:

                               You can remove query string in address bar using .htaccess. There you can pass values in address bar. Consider the following example.

               RewriteRule ^example/(.*)$ example.php?ex=$1 [L]
                     it avoids the normal rule.
      ie, example.php?ex=something
                           when the browser get, it considered as Then you can get this value by $_GET['ex']. In this method, you can make your urls very simple and it is easily remembered by any browser.
                            ^  - starting point
                            .* -  one or more characters
                            $  - end point
                            L  - tells the server to stop trying to rewrite a URL after it has applied that rule.

How to redirect one web page url to another website:

                            Suppose you may have one more sites. If you feel when the users try to access the particular file, it directs to another your website.You can also redirect the urls from one website to another. It can be possible in .htaccess.

                Redirect  302 / site2.php
                    Where,  302 is the temporary redirect.
                                  When the user access the site2.php file in your current domain, it redirect to website.    

How to redirect from one site to another:

                                    Suppose you feel when the user access your one site, it redirect to another site. Then it can be done by .htaccess.

              Redirect 302 /

              Now if the user come to your current domain, it will redirect to

Related Post:

25 Nov 2013

How to enable or disable the directory lists using .htaccess

                      Your public_html folder should be contain many directories like images, js, styles. Someone may be try to access these files from your site without your permission. If you think, don't allow others to access these files, then you can create .htaccess file for protect your files from them.

               Options -Indexes
                IndexIgnore *

           It tells the server to disable the directory listings on your site. Now you can not see your directory lists. If you try to get access it, then you will get forbidden error message.

You can also enable this features to your sire.
               Options +Indexes
           It tells the server to enable the directory listings on your site.

How to ignore the particular file based on its extensions in directory lists using .htaccess:

                        Normally when you access the directory list on your browser, you can get all files presented in that directory. Sometimes, you may be feel, other files will display to user except .php, .css, .js. you can done by .htaccess.

             IndexIgnore .*php .*css .*js

                       Now the user can get access your files except the file with extension php, js and css

How to display the directory lists with other details using .htaccess:

                      By default, the directory lists are displays just with only its name. If you want to know the additional details like its size, description, then you can done by .htaccess.

              Indexoptions +FancyIndexing
                 Now you can get directory lists with additional information such as size, description, last modified date etc..

You can also disable the additional details.

              Indexoptions -FancyIndexing

24 Nov 2013

How to prevent access to php.ini file using .htaccess

                     Php.ini file is the configuration file. Any changes in php.ini file can affect the functionalities of php. It is read each time the php is initialized. By default, php.ini file is located in /user/local/lib directory on your server.  Max_execution_time, shot_open_tag, memory_limit, allow_url_fopen, PHP_INI_SYSTEM, PHP_INI_ALL, PHP_INI_PERDIR are some of the modes present in a php.ini configuration file. It can be easily access by others . We can restrict this by .htaccess.
        <FilesMatch "\.(js | jpeg | jpg)$">
             Order allow, deny
             Deny from All
             Allow from env=REDIRECT_STATUS

                            You can add any file type by type extension without '.' and each one is separate by '|' .

       <Files php.ini>
          Order allow, deny
          Deny from all
                      Now your php.ini file is invisible to viewers.

Related Post:

22 Nov 2013

How to force script to display as source code using .htaccess

                       You can display script code as a source code instead of executing.
             RemoveHandler cgi-script .pl .cgi .php .py
             AddType text/plain .pl .cgi .php .py

                        RemoveHandler - remove all the server-parsed handler associated with the extensions like .pl, .php for all the paths.
                        .cgi - run outside of /cgi-bin/ directory
                        .py -  treat same as a normal HTML file
                        AddType - used to assign a MIME type to a file suffix
                        text/plain - format of file.

Related Post:

21 Nov 2013

How to enable SSI using .htaccess

                      SSI expands Server Side Includes. It is used to allow you can include in your html doucments for call CGI scripts or html documents. Due to this, space of disk is saved.

              AddHandler server-parsed .html
                   It tells the server to allow the server side includes to your content which have the extensions '.html' . But SSI does slow down the server. Because it need to extra stuff before serving up a page

Related Post:

20 Nov 2013

How to add MIME types using .htaccess

                      MIME expands  Multi-purpose Internet Mail Extensions. It is used to classify the file types on Internet.
                     Sometimes, the browser can not understand the file types of a file. At that time, we can instructs to browser, what file type is? using .htaccess. We can also create custom file type using it.
            AddType text/plain .txt
                        AddType - means that you are adding a new file type to MIME types.
                        text/plain  - MIME type
                                .txt   - extensions
                 AddType application/pdf .pf
                 AddType application/zip .zip
                 AddType image/jpeg .jpg .jpeg

Most Common MIME Types:


     .3dm                - x-world/x-3dmf
      .3dmf               - x-world/x-3dmf
      .a                     - application/octet-stream
      .aab                 - application/x-authorware-bin
      .aam                - application/x-authorware-map
      .aas                 - application/x-authorware-seg
      .abc                 - text/
      .acgi                - text/html
      .afl                   - viedo/animaflex
      .aif                  - audio/aiff
      .aif                  - audio/x-aiff
      .aifc                 - audio/aiff
      .aif                  - audio/x-aiff
      .aiff                  - audio/aiff
      .aiff                  - audio/x-aiff
      .aim                  - application/x-aim
      .aip                  - text/x-audiosoft-intra
      .ani                  - application/x-navi-animation
      .aos                  - application/x-nokia-9000-communicator-add-on-software
      .aps                  - application/mime
      .arc                  - application/octet-stream
      .arj                  - application/arj
      .arj                  - application/octet-stream
      .art                  - image/x-jg
      .asf                  - video/x-ms-asf
      .asm                - text/x-asm
      .asp                  - text/asp
      .asx                  - application/x-mplayer2
      .asx                  - video/x-ms-asf
      .asx                  - video/x-ms-asf-plugin
      .au                  - audio/basic
      .au                  - audio/x-au
      .avi                 - application/x-troff-msvideo
      .avi                 - video/avi
      .avi                 - video/msvideo
      .avi                 - video/x-msvideo
      .avs                 - video/avs-video
      .bcpio             - application/x-bcpio
      .bin                 - application/mac-bimary
      .bin                 - application/macbinary
      .bin                 - application/octet-stream
      .bin                 - application/x-binary
      .bin                 - application/x-macbinary
      .bm                 - image/bmp
      .bmp               - image/bmp
      .bmp               - image/x-windows-bmp
      .boo                - application/book
      .book              - application/book
      .boz                 - application/x-bzip2
      .bsh                 - application/x-bsh
      .bz                   - application/x-bzip
      .bz2                 - application/x-bzip2
      .c                     - text/plain
      .c                     - text/x-c
      .c++                - text/plain
      .cat                  - application/
      .cc                   - text/plain
      .cc                   - text/x-c
      .ccad               - application/clariscad
      .cco                 - application/x-cocoa
      .cdf                  - application/cdf
      .cdf                  - application/x-cdf
      .cdf                  - application/x-netcdf
      .cer                  - application/pkix-cert
      .cer                  - application/x-x509-ca-cert
      .cha                  - application/x-chat
      .chat                 - application/x-chat
      .class                - application/java
      .class                - application/java-byte-code
      .class                - application/x-java-class
      .com                 - application/octet-stream
      .com                 - text/plain
      .conf                 - text/plain
      .cpio                 - application/x-cpio
      .cpp                  - text/x-c
      .cpt                   - application/mac-compactpro
      .cpt                   - application/x-compactpro
      .cpt                   - application/x-cpt
      .crl                    - application/pkcs-crl
      .crl                    - application/pkix-crl
      .crt                    - application/pkix-cert
      .crt                    - application/x-x509-ca-cert
      .crt                    - application/x-x509-user-cert
      .csh                   - application/x-csh
      .csh                   - text/x-script.csh
      .css                   - application/x-pointplus
      .css                   - text/css
      .cxx                   - text/plain
      .dcr                   - application/x-director
      .deepv               - application/x-deepv
      .def                   - text/plani
      .der                   - application/x-x509-ca-cert
      .dif                    - video/x-dv
      .dir                    - application/x-director
      .dl                     - video/dl
      .dl                     - video/x-dl
      .doc                  - application/msword
      .dot                  - application/msword
      .dp                   - application/commonground
      .drw                 - application/drafting
      .dump              - application/octet-stream
      .dv                   - video/x-dv
      .dvi                  - application/x-dvi
      .dwf                 - drawing/x-dwf (old)
      .dwf                 - model/vnd.dwf
      .dwg                - application/acad
      .dwg                - image/vnd.dwg
      .dwg                - image/x-dwg
      .dxf                  - application/dxf
      .dxf                  - image/vnd.dwg
      .dxf                  - image/x-dwg
      .dxr                  - application/x-director
      .el                    - text/x-script.elisp
      .elc                  - application/x-bytecode.elisp
      .elc                  - application/x-elc
      .env                 - application/x-envoy
      .eps                 - application/postscript
      .es                   - application/x-esrehber
      .etx                  - text/x-setext
      .evy                 - application/envoy
      .evy                 - application/x-envoy
      .exe                 - application/octet-stream
      .f                     - text/plain
      .gif                  - image/gif
      .gl                   - video/gl
      .gsm               - audio/x-gsm
      .gz                  - application/x-compressed
      .gz                  - application/x-gzip
      .gzip               - application/x-gzip
      .gzip               - multipart/x-gzip
      .htc                 - text/x-component
      .htm                - text/html
      .html               - text/html
      .htmls              - text/html
      .htx                 - text/html
      .ico                 - image/x-icon
      .ima                - application/x-ima
      .imap              - aplication/x-httpd-imap
      .jav                 - text/x-java-source
      .java               - text/plain
      .java               - text/x-java-source
      .jpe                 - image/jpeg
      .jpe                 - image/pjpeg
      .jpeg                 - image/jpeg
      .jpeg                 - image/pjpeg
      .jpg                 - image/jpeg
      .jpg                 - image/pjpeg
      .js                   - application/x-javascript
      .js                   - application/javascript
      .js                   - text/javascript
      .ksh                - application/x-ksh
      .list                 - text/plain
      .m1v              - video/mpeg
      .m2a              - audio/mpeg
      .m3u              - audio/x-mpequrl
      .mht               - message/rfc822
      .mhtml           - message/rfc822
      .mif               - application/x-iframe
      .mif               - application/x-mif
      .mime            - message/rfc822
      .mime            - www/mime
      .mod             - audio/mod
      .mod             - audio/x-mod
      .movie           - video/x-sgi-movie
      .mp2              - audio/mpeg
      .mp2              - audio/x-mpeg
      .mp2              - video/mpeg
      .mp2              - video/x-mpeg
      .mp2              - video/x-mpeg2a
      .mp3              - audio/mpeg3
      .mp3              - audio/x-mpeg3
      .mp3              - video/mpeg
      .mp3              - video/x-mpeg
      .nif                 - image/x-niff
      .niff                 - image/x-niff
      .omc               - application/x-omc
      .omcd             - application/x-omcdatemaker
      .omcr              - application/x-omcregerator
      .part                - application/pro-eng
      .pas                 - text/pascal
      .pic                  - image/pict
      .pict                 - image/pict
      .png                 - image/png
      .ppm                - image/x-portable-pixmap
      .pps                  - application/mspowerpoint
      .ppt                  - application/powerpoint
      .qcp                 - audio/vnd.qcelp
      .rpm                 - audio/x-pn-realaudio-plugin
      .rt                     - text/richtext
      .rtf                    - application/rtf
      .sdp                  - application/sdp
      .sgm                  - text/sgml
      .sgm                  - text/x-sgml
      .sgml                 - text/sgml
      .sgml                 - text/x-sgml
      .shtml                - text/html
      .spr                   - application/x-sprite
      .sprite               - application/x-sprite
      .src                   - application/x-wais-source
      .swf                  - application/x-shockwave-flash
      .tex                   - application/x-tex
      .texi                  - application/x-texinfo
      .texinfo             - application/x-texinfo
      .text                  - application/plain
      .text                  - text/plain
      .txt                    - text/plain
      .unv                  - application/i-deas
      .vcd                  - application/x-cdlink
      .voc                  - audio/voc
      .vos                  - video/vosaic
      .wav                 - audio/wav
      .wav                 - audio/x-wav
      .wmf                 - windows/metafile
      .wpd                 - application/wordperfect
      .wsc                  - text/scriplet
      .xl                      - application/excel
      .xla                    - application/excel
      .xla                    - application/x-excel
      .xla                    - application/x-msexcel
      .xlb                    - application/excel
      .xm                    - application/xml
      .z                       - application/x-compress
      .z                       - application/x-compressed
      .zip                    - application/x-compressed
      .zip                    - application/x-zip-compressed
      .zip                    - application/zip
      .zip                    - application/x-zip

Related Post:

19 Nov 2013

How to add expires headers using .htaccess

                      Check your site in  google insights page speed. You will get a error "Leverage Browser Caching", if your server doesn't add caching headers to your site. If you add expires headers to your .htacces file, then you will get better site speed. Expires headers tells browser whether they should request file from browser or whether they should grab it from browser's cache. Its not only reduce the loads of download from server but rahter to reduce the number of http requests for the server.
        <IfModule mod_expires.c>
             # Enable Expiration
                ExpiresActive On

             # Default Directive
                ExpiresDefault "access plus 1 month"

             # Favicon
                ExpiresByType image/x-icon "access plus 1 month"

             # Images
                ExpiresByType image/jpeg "access plus 1 month"
                ExpiresByType image/jpg "access plus 1 month"
                ExpiresByType image/png "access plus 1 month"
                ExpiresByType image/gif "access plus 1 month"

             # Css
                ExpiresByType text/css "access plus 1 month"

             # Javascript
                ExpiresByType application/javasacript "access plus 1 month"

             # Html
                ExpiresByType text/html "access plus 1 month"

             # Xml
                ExpiresByType application/xhtml+xml "access plus 1 month"

             # Manifest files
                ExpiresByType application/x-web-app-manifest_json "access plus 1 month"
                ExpiresByType text/cache-manifest "access plus 1 month"

             # Media
                ExpiresByType audio/ogg "access plus 1 month"
                ExpiresByType video/mp4 "access plus 1 month"
                ExpiresByType video/ogg "access plus 1 month"
                ExpiresByType video/webm "access plus 1 month"

             # Web feeds
                ExpiresByType application/atom+xml "access plus 1 month"
                ExpiresByType application/rss+xml "access plus 1 month"

             # Web fonts
                ExpiresByType application/font-wof "access plus 1 month"
                ExpiresByType application/ "access plus 1 month"
                ExpiresByType application/x-font-ttf "access plus 1 month"
                ExpiresByType font/opentype "access plus 1 month"
                ExpiresByType image/svg+xml "access plus 1 month"

                   mod_expires.c allows setting by file type to control how long browsers cache file.
                   ExpiresActive enable the expires.
                   ExpiresByType represents the type of file like image, html ...
                   "access + 1 month" means how long the file type cached by browser.

Related Post:

18 Nov 2013

How to create error document using .htaccess

                      All of you should be get error message from your browser. When you type new page name in your address bar, you will get file not found error if it is not in your site. By default, the browser send this type of error message. But you can create your own error page instead of browser's error message. It can be done by .htaccess. Due to this, your site get strange from others sites.

               ErrorDocument 404  /404.php
 When site get 404 (ie, file not found) error, file 404.php will run.
Server have lot of error codes like 404. They are following as:
             100 - Continue
             101 - Switching Protocols
             200 - OK
             201 - Created
             202 - Accepted
             203 - Non-Authoritative Information
             204 - No Content
             205 - Reset Content 
             206 - Partial Content
             300 - Multiple Choices
             301 - Moved Permanently
             302 - Found
             303 - See Other
             304 - Not Modified
             305 - Use Proxy
             307 - Temporary Redirect
Client Error
             400 - Bad Request
             401 - Unauthorized
             402 - Payment Required
             403 - Forbidden
             404 - Not Found
             405 - Method Not Allowed
             406 - Not Acceptable
             407 - Proxy Authentication Required
             408 - Request Timeout
             409 - Conflict
             410 - Gone
             411 - Length Required
             412 - Precondition Failed
             413 - Request Entity Too Large
             414 - Request-URI Too Long
             415 - Unsupported Media Type
             416 - Requested Range Not Satisfiable
             417 - Expectation Failed
Server Error
             500 - Internal Server Error
             501 - Not Implemented
             502 - Bad Gateway
             503 - Service Unavailable
             504 - Gateway Timeout
             505 - HTTP Version Not Supported

Related Post:

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.
                <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
                         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.*

You can also done by your php file. Just add this to your header file.
           if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')) ob_start('ob_gzhandler');
After add this content to your .htaccess file, you can check whether your site is compressed or not in  website.

Related Post: