Technical Support: Manipulating Your Website with HTACCESS

by Chris on June 11, 2010

in Technical Support

You can manipulate the files and what happens with different files at your website or blog host with what is known as an .HTACCESS file.

CAUTION#1

This is some very powerful stuff and you can easily bring down your website quickly. Always make back-ups (yes, multiple backups) of your .HTACCESS file before you make any modifications!

CAUTION #2

Unless you are pretty technically inclined, this is probably NOT stuff you should be doing. I put this information here for reference only. I highly recommend you do more research into managing .htaccess files before just “cutting and pasting” what you see below.

Point an entire site to a different URL, such as domain.net redirected to domain.com:

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://www.anotherwebsite.com/

Redirect index.html to a specific subfolder:

# This allows you to redirect index.html to a specific subfolder
Redirect /index.html http://www.anotherwebsite.com//newdirectory/

Redirect an old file to a new file path:

# Redirect old file path to new file path
Redirect /olddirectory/oldfile.html http://www.anotherwebsite.com//newdirectory/newfile.html

Redirect to a specific index page:

# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html

Redirect viewers of your domain to use the secure version of your domain:

# An easy to way to always redirect the user to secure connection (https://) can be accomplished with the following lines:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R,L]

Redirect users to www for http:// and https://

RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]

Redirect users to use https:// for a particular folder:

# In case you wish to force HTTPS for a particular folder you can use the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} somefolder
RewriteRule ^(.*)$ https://www.yoursite.com/somefolder/$1 [R,L]

Redirect users to access the site without www:

# To redirect all users to access the site WITHOUT the www. prefix,
# (http://www.example.com/… will be redirected to http://example.com/…)
# adapt and uncomment the following:

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

Redirect users to use www:

# To redirect all users to access the site WITH the www. prefix,
# (http://example.com/… will be redirected to http://www.example.com/…)
# adapt and uncomment the following:

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

Related Articles:

Subscribe to my weekly newsletter and I’ll tell you how to book more weddings and
make fewer mistakes with your Internet marketing.

Got a question about Internet marketing? Think you’re getting some bad advice? Are you a Do-It-Yourselfer and stuck? Drop me a line here.

Leave a Comment

Previous post:

Next post: