Htaccess (Hypertext Access) files are used past times Apache Webserver to command the configuration of the directory inwards which your website resides. One tin easily configure safety settings without restarting the server.
Almost every hosting provider supports htaccess. One uncomplicated manner to examination whether your host provides .htaccess files is to banking corporation tally whether at that topographic point is a back upwards for password protection of folders. You tin likewise easily exercise your ain .htaccess file as well as upload it inwards your directory. Apply a few rules on it as well as larn the best results for your website.
The .htaccess file is exactly an extension amongst no filename. On about of the operating systems, y'all may expression upwards problems inwards uploading as well as searching the file. In such cases, y'all tin exactly rename the file later uploading it on the server.
The diverse applications of the .htaccess file are every bit follows :
1. Password protecting directories.
Every subdirectory as well as file volition locomote protected inside the given directory.
Note that the htaccess file rules are followed from superlative to bottom.
In the .htpasswd file, write the username as well as password inwards the next format :
> 2. Alowing or Disallowing Access to Certain IP Addresses
In companionship to trammel users amongst item IP addresses, add together the next directives inwards the .htaccess file :
Use the next directive to redirect a directory to about other directory or a page to about other page.
#Redirect from the electrical current url eg. http://www.current.com/currentdir to http://www.new.com/newdir
4. Handling Errors Using Custom Error Pages
Eg. For a 404 Page non constitute error, 1 tin write the next code :
To disallow file listing inwards a directory, y'all tin role next directive :
Some host servers may non locomote setup to handgrip specific file types such every bit mp3 or swf files.
This tin locomote easily fixed using “AddType”
For this, 1 tin role rewrite rules to exercise SEO friendly URL names.
If y'all accept a dynamic page called /category_mobile.php as well as it is passed a numeric variable called id inwards the shape of /category_mobile.php?id=2, y'all tin role .htaccess to exercise a to a greater extent than SEO-friendly URL construction such every bit /category-mobile/2.
To make this, use:
9. Protecting y'all .htaccess file
Your .htaccess file tin locomote hacked as well as code tin locomote inserted to redirect to about other website etc.
1. Add the next to your .htaccess file to protect it:
2. Set the .htaccess file permission to 644
Almost every hosting provider supports htaccess. One uncomplicated manner to examination whether your host provides .htaccess files is to banking corporation tally whether at that topographic point is a back upwards for password protection of folders. You tin likewise easily exercise your ain .htaccess file as well as upload it inwards your directory. Apply a few rules on it as well as larn the best results for your website.
The .htaccess file is exactly an extension amongst no filename. On about of the operating systems, y'all may expression upwards problems inwards uploading as well as searching the file. In such cases, y'all tin exactly rename the file later uploading it on the server.
The diverse applications of the .htaccess file are every bit follows :
- Password protecting directories.
- Allowing or Disallowing access to surely IP addresses.
- Redirect Users to about other Urls.
- Configure custom mistake pages.
- Stop Directory Listing.
- Using unlike files every bit index files.
- Add additional MIME types.
- URL Rewriting (Best feature)
- Protecting y'all .htaccess file
1. Password protecting directories.
Every subdirectory as well as file volition locomote protected inside the given directory.
Note that the htaccess file rules are followed from superlative to bottom.
# Directory advert to locomote protected AuthName "Directory Name" AuthType Basic # Absolute path to the file storing password AuthUserFile /path/.htpasswd Require valid-userIf possible, house the password file ever exterior the populace directory.
In the .htpasswd file, write the username as well as password inwards the next format :
myuser:mypasswordNote: Username as well as password should locomote separated using “:”
> 2. Alowing or Disallowing Access to Certain IP Addresses
In companionship to trammel users amongst item IP addresses, add together the next directives inwards the .htaccess file :
# deny all other except the ip address 10.10.10.10 <Limit GET POST PUT> companionship deny,allow deny from all let from 10.10.10.10 let from .*abc.com.* </Limit>3. Redirect URLS
Use the next directive to redirect a directory to about other directory or a page to about other page.
#Redirect from the electrical current url eg. http://www.current.com/currentdir to http://www.new.com/newdir
Redirect /currentdir http://www.new.com/newdir
4. Handling Errors Using Custom Error Pages
Eg. For a 404 Page non constitute error, 1 tin write the next code :
ErrorDocument 404 /pagenotfound.html5. Stop Directory Listing
To disallow file listing inwards a directory, y'all tin role next directive :
Options -Indexes6. Using a Different File every bit a Index File
DirectoryIndex my.php index.php pictures.pl index.html default.htm7. Add Additional MIME Types
Some host servers may non locomote setup to handgrip specific file types such every bit mp3 or swf files.
This tin locomote easily fixed using “AddType”
# define diverse MIME types8. URL Rewriting SEO skillful ever advise to display the principal keyword inwards the URL.
AddType application/x-shockwave-flash .swf
AddType application/octet-stream .mp3
For this, 1 tin role rewrite rules to exercise SEO friendly URL names.
If y'all accept a dynamic page called /category_mobile.php as well as it is passed a numeric variable called id inwards the shape of /category_mobile.php?id=2, y'all tin role .htaccess to exercise a to a greater extent than SEO-friendly URL construction such every bit /category-mobile/2.
To make this, use:
RewriteEngine onYou tin likewise perform many other neak tricks similar Browser sniffing, Saving bandwidth, Force Caching, Magic Urls using .htaccess.
RewriteRule ^/?category-mobile/([0-9]+)$ /category-mobile.php?id=$1
9. Protecting y'all .htaccess file
Your .htaccess file tin locomote hacked as well as code tin locomote inserted to redirect to about other website etc.
1. Add the next to your .htaccess file to protect it:
# Protect the htaccess file <files .htaccess> companionship allow,deny deny from all </files>
2. Set the .htaccess file permission to 644