Password protection in .htaccess

To password protect a folder by using .htaccess file;

# vi /home/username/.htpasswds/.htpasswd

BBC:T52pU9TTVjMXQ

ie

user:md5password

then create a .htaccess file in respective folder which you want to protect

# vi .htaccess

and add following

AuthUserFile /home/username/.htpasswds/.htpasswd

AuthType Basic

AuthName “Login Details”

Require valid-user

the path depends on the location of the .htpasswd file.

Directory Listing options in .htaccess

If there is no index page in a directory of your website, anyone can see the files in that directory. It’s called as Directory Listing.

1. To Enable/Disable Directory Listing

To have the web server produce a list of files for such directories, use the below line in your .htaccess.

Options +Indexes

To have an error (403) returned instead, use,

Options -Indexes

2. Listing Style

Either a basic list of files can be shown, or a ‘fancy’ list including icons, file size, modification date and more.

IndexOptions +FancyIndexing

Add this to your .htaccess file to use the ‘fancy’ style.

IndexOptions -FancyIndexing

Use the above line if you prefer a more basic file list.

3. Ignore Files

Let’s say you have a directory with .jpg, .gif and .png image files. You only want the .png files to show in the directory listings. Add this line to your .htaccess.

IndexIgnore *.gif *.jpg

The web server now ignores the .gif and .jpg files.

4. Modify Index File

Maybe you don’t want a list of the files, you want a specific file to be shown instead. You could upload an index.html file in this directory. There is another way.

DirectoryIndex myfile.html

Instead of listing the files, web page myfile.html will now be shown for this directory and its sub-directories.

how to set up redirects by using .htaccess

To redirect a domain.com to www.domain.com

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

To redirect a subdomain sub.domain.com to www.newdomain.com

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^sub.domain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]

Redirecting a website in PHP

<?php
header( ‘Location: http://www.domain.com/new_page.html’ ) ;
?>

You need to add provided code in your index.php file. it will be redirected to http://www.domain.com/new_page.html.