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 https://www.mywebsite.com.
Now you accessed newSite by URL https://www.mywebsite.com/newSite/
If you have hosted a domain named as https://www.newSite.com
And you want that all incoming traffic to https://www.mywebsite.com/newSite/ should redirect to https://www.newSite.com

I mean https://www.mywebsite.com/newSite/ to https://www.newSite.com

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} ^newSite.mywebsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.newSite.mywebsite.com$
RewriteRule ^(.*)$ https://www.newSite.com [R=301,L]

OR

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

These methods should redirect all traffic from https://www.mywebsite.com/newSite/ to https://www.newSite.com. This is permanent redirect.

Ok now we move ahead:

How about No directory listing across whole domain?
and
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 xyz.com and you want all of its traffic to redirect to abc.com that is also yours of course.
Then its easy with .htaccess

Temporary Redirect

# Redirect all traffic from Domain
RedirectMatch temp .* https://www.newwebsitename.com
# Redirect end

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

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: