Htaccess With Few examples

Htaccess With Few examples

Htaccess(Hypertext Access) is a directory-level configuration file for Apache.The use of the .htaccess files is slow down your Apache server performance.htaccess is mainly used for the Apache users who don’t have permission to modify Apache configuration file.If u having access to httpd main server configuration file avoid .htaccess files completely.

Few .htaccess rules

Password protection

Make the user enter a name and password before viewing a directory.

AuthUserFile /home/example/public_html/.htpasswd-username
AuthName “Protected Directory”
AuthType Basic
<limit GET POST>
require Valid user
</limit>

Deny users by IP address and allow others

Order allow,deny
Deny from 192.168.5.23
Allow from all

Allow users by IP address and deny other

Order deny,allow
Deny from all
Allow from 192.168.5.23

Rewrite Engine

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Its used to redirect http://example.com to http://www.example.com

Redirect

Redirect 301 /linux/htaccess http://www.example.com/linux/htaccess

To redirect http to https

ssl certificate is installed in server and we want to redirect all requests to https we use the following rule in .htaccess

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://www.domain.com [L,R]

To set the administrator email in .htaccess

SetEnv SERVER_ADMIN admin@example.com

Turn off the Server Signature

ServerSignature Off

Serversignature is used to protect from user to view the Apache version,its usually appears on error page.

To prevent directory listings

IndexIgnore *

 




Leave a Reply

Your email address will not be published. Required fields are marked *