WHAT'S NEW?
Loading...

Working with .htaccess

.htaccess



                we can all agree that pretty URLs make things easier for our users and adds a level of professionalism and polish to any web application. We must accept that twitter is widely popular web application and a part for reason for that is most certainly how its formats URL.

                .htaccess file is directory-Level configuration that allows decentralized management of web server configuration. It Simply means hypertext access. The purpose of it is to allow pre-directory access control. For example, to set password to access content of web server or to deny user to access certain files.
.htaccess files are stored inside web tree. Some of the work of .htaccess file is as below.


* Rewriting URLS.
                To replace or remove specific content of URL.

* Blocking
                Block users, domains, IP addresses by using deny / allow.

* Directory listing & Customized Error response
                Controls how server react when specific web page not available Ex. Pages like 404 & 500

Here we will learn some tricks to create user friendly URL.

1) Remove extension of PHP/HTML file and make readable URL that contain queries.

URL

.htaccess
RewriteEngine on
RewriteRule ^/index/([0-9]+)\.html /index.php?index_id=$1                 

Result


2) Remove particular content from URL using .htaccess

URL
Mysite.com/folder/test.php

We will use two .htaccess file to remove content `folder` from URL

-> Goto `root/folder/.htaccess`

RewriteEngine on
RewriteCond %{request}^[A-Z]{3,}\s/+folder([^\s]*) [NC]
RewriteRule ^%1 [R=301,L]


-> Goto `root/.htaccess`
RewriteEngine on
RewriteRule !^/?folder folder%{REQUEST_URI} [L,NC]


3) Block particular domain

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} domainname\.com [NC]
RewriteRule .* - [F]

4) allow/block particular IP address

-> Block all
     deny all               
-> allow/Block Specific IP address
    order allow,deny
    allow from all
    deny from 120.177.166.165
-> block IP address Range
    deny from 123.123.123

5) Limit the Number of Concurrent Visitors to your Website
    MaxClients <Number of max clients>


6) Deny access to folder during particular time

    RewriteEngine On
    # Then deny all access
    RewriteRule ^.*$ - [F,L]
    # If the hour is 16 (4 PM)
    RewriteCond %{TIME_HOUR} ^16$
    # Multiple hour blocks
    # If the hour is 4 PM or 5 PM or 8 AM
    RewriteCond %{TIME_HOUR} ^16|17|08$

7) Redirect specific URL to another URL

Redirect /path/folder/1/2/3 http://www.musite.com/newpath/1/2/3



0 comments:

Post a Comment