.htaccess

Fast, reliable, and now featuring free SmarterMail with all Linux Cloud Spaces Plans
 
.htaccess Redirect Assistance

A .htaccess file consists of several lines of code that is a popular tool use to manipulate the way URLs are displayed on a website, or to redirect a URL to a particular web page or directory. Rewrite rules are placed within a .htaccess file (which is then placed in the root of your site or directory) to instruct the rewrite engine how to respond when a specified URL is entered into a web browser that needs to be changed or redirected. A .htaccess file can be used for many reasons including:
  • Redirecting a domain to a sub folder
    • Ex. Redirect apple.com to orange.com/apple
  • Permanently redirecting traffic to a different domain name (301 Redirect)
    • Ex. Have all traffic apple.com go to orange.com and have orange.com show up in the address bar.
  • Changing the Top Level Domain Name
    • Ex. When someone types orange.net, orange.com automatically shows in the address bar.
  • Changing the Extension
    • Ex. When someone types in index.html it will automatically change to index.aspx
  • Adding/Removing www from the address
    • Ex. When you need to force if www is used or not for things like SSL use.
  • Forcing a Secure connection (https://)
    • Ex. When you need to use an SSL to secure your entire site and force https://

All .htaccess rules are made possible by IIS Mod-Rewrite, an installed component available on select hosting plans.  IIS Mod-Rewrite emulates, on IIS web servers, the same functionality as Apache mod_rewrite. Click here to see if your hosting plan supports IIS Mod-Rewrite.


.htaccess Script Generator



We have a generator for the most common uses of a .htaccess file. Simply fill out the applicable form and click Generate. Our script generator can be found here or by typing http://media.newtektechnologyservices.com/htaccess_kb into your browser.

Once you generate the code, you'll need to copy and paste it onto your site's .htaccess file. If you do not have a .htaccess file, simply save the code into a text editor and save it as ".htaccess" (DO NOT add any other extension to the file, such .txt or .html) in the root of your site.

Upload the .htaccess file, via FTP, to the root of your website.

If you feel familiar with scripting and would like to manually configure your .htaccess file please use the following resources. 


Manual 301 Redirect


301, in HTTP protocol, is a status code interpreted as "moved permanently."  It is to be used in the case where you have a web page that has been moved to a different directory, for example, a 301 redirect is an effective way to route the browser to the "new" location in a SEO friendly manner.

We have created some example code to quickly implement 301 redirects on your website.  Although there are many different ways to write rewrite rules for a desired outcome, the below examples have been successfully tested in our hosting environment.

Redirecting traffic to an entirely new domain name

    #
    RewriteEngine On
    RewriteRule ^(.*)$ http://www.domain-2.com/$1 [R=301,L]
    #

Redirecting traffic from one folder to another

   #
   RewriteEngine On
   RewriteRule ^directory-1/(.*)$ http://www.example.com/directory-2/$1 [R=301,L]
   #

 

Redirecting a domain name to a sub folder

   #
   RewriteEngine On
   RewriteCond %{HTTP_HOST} example.com
   RewriteCond %{REQUEST_URI} !directory/
   RewriteRule ^(.*)$ directory/$1 [L]
   #

 

Redirect a file to a file

   #
   RewriteEngine On

   RewriteRule file1.htm http://www.exampledomain.com/file2.htm [R=301]
   #

 

Force https for a certain file or directory
 
   # File:
   RewriteEngine On
   RewriteCond %{SERVER_PORT} !^443$
   RewriteRule ^give_checkout\.aspx$ https://www.goodwillny.org/give_checkout.aspx [R=301,L]
 
   #Directory:
   RewriteEngine On
   RewriteCond %{SERVER_PORT} !^443$
   RewriteRule ^DIRECTORY$ https://DOMAIN.org/DIRECTORY [R=301,L]
   #
 
 
Force https for all requests.
 
   #
   RewriteEngine On
   RewriteCond %{SERVER_PORT} 80
   RewriteRule ^(.*)$ https://www.DOMAIN.com/$1 [R,L]
   #



A 301 redirect can also be used to add or remove the www in front of your domain name.  This is helpful where there is an SSL certificate installed on your site because typically an SSL only secures 1 version of the domain name.  The secured domain name typically does or does not include www.  To avoid purchasing 2 SSL certificates you can use the following example:

Forcing WWW or Non-WWW

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


SEO Friendly URL Generation



SEO Friendly URLs, also known as  Pretty Permalinks or Flat URLs, is a way to make your site easier for your customers and search engines like Google, Yahoo, and Bing to search your site.  It will take a URL that looks like:

http://www.website.com/products.php?articleid=13&section=2

And change it to:

http://www.website.com/product/article/

Obviously this makes things look much simpler and easier to remember.  For guidance on how to take advantage of Mod-Rewrite for some popular applications, please see the following Knowledge Base Articles:

SEO Friendly URLs for WordPress:
KB 755

SEO Friendly URLs for Joomla:
KB 754

SEO Friendly URLs for Drupal:
KB 757


 

Special Notes for Server 2008

 



On our Windows Server 2008 plans you will need to enable Mod-Rewrite in order to use a .htaccess file.  Instructions on how to enable mod-rewrite can be found in KB 1499.

 

Add Feedback