Essential DotHtaccess Hacks

by Hiroshi on September 29, 2008

in htaccess

.htaccess rules can be different for different hosts. This is file in root directory of website which defines certain rules for website behavior for some requests.

Redirect From A Directory/Folder/Subdomain to Domain With Wild Card Support

Suppose you had a subdomain in your account named as newSite.
Your primary domain is
Now you accessed newSite by URL
If you have hosted a domain named as
And you want that all incoming traffic to should redirect to

I mean to

Then you need to define a rule in .htaccess file and place it in the root of primary website domain.
The name is exact .htaccess

Permanent Redirect Rule Code

RewriteEngine on
RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$ [R=301,L]


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

These methods should redirect all traffic from to This is permanent redirect.

Ok now we move ahead:

How about No directory listing across whole domain?
Disallow all hidden files across domain?

No directory listing (Indexes Off) and Disallow Hidden Files at Host – Unix Based Host

# No directory listing across domain
Options -Indexes
# Disallow hidden files
RedirectMatch 403 /\..*$

Put above code in the .htaccess file to disallow hidden files and to turn off indexes.
This is for security reasons of your account.
You see nobody should be able to browse or see contents of any directory which has no index.html or index.php file and also he should not see hidden host files i.e. logs.

Now Another Interesting Example:

Suppose following cases:
if you want to redirect from one domain to another.
You want to temporarily disable any website.
You want that temporarily nobody should be able to access your website.
You have got another domain name or registration lets say and you want all of its traffic to redirect to that is also yours of course.
Then its easy with .htaccess

Temporary Redirect

# Redirect all traffic from Domain
RedirectMatch temp .*
# Redirect end

.htaccess file containing this code should be placed in root of website which you want to redirect to

Some web hosts which ban your website for some reason or disable your account for any reason also do this. They place .htaccess in your account root directory, I mean in / and not in www. And they give re directive path to their disable page showing some message like you have been scr***d up while you never knew. hehe!

Fine… end of story.

Warning: If these examples are not for advanced users, then these might not also be for beginners. Please consult your web host support for this kind of configuration to your server or prior to doing these.

Related Posts

Previous post:

Next post: