Knowledgebase: Plesk 11

How to Configure Redirects via an .Htaccess File

Posted by Justin Davis on September 01 2015 04:05 PM

An .htaccess (hypertext access) file is a configuration file that allows server configuration changes on a per-directory basis. The .htaccess file will override all global configurations for the directory in which it is located, as well as for all subdirectories. Common uses of the .htaccess file include: redirects, custom error pages, and PHP settings. Before making any changes to your .htaccess file, we strongly suggest making a backup copy of the file to allow easy restoration, if needed. This is because a single typo in the .htaccess file can cause your entire website to show an internal server error.

To create an .htaccess file, you can simply create a text document in an editor such as Notepad or Notepad++, and save the file as ".htaccess". Any of th examples below will be added to a new line in the file, then saved. Once your .htaccess file is created, you will need to upload it to the folder in which you wish for the file to take effect. If you are not familiar with uploading files to your server, please review the following articles: Where to Upload Site Files via FTP, Using an FTP Program, or How to Upload or Download Files Using File Manager in the Control Panel

  • Redirect a single file to a new file on same domain: To redirect a single file such as http://yourdomain.com/index.html to http://yourdomain.com/newindex.php,  you can perform a redirect like the one below. This will redirect any call for index.html to testindex.php.

Redirect 301 /index.html /newindex.php

  • Redirect a single file to a new file on a different domain:  To redirect a single file such as http://yourdomain.com/index.html to http://yourdomain.net/newindex.php, you can perform a redirect like the one below.

Redirect 301 /index.html http://yourdomain.net/newindex.php

  • Redirect one domain name to another: To redirect all traffic from one domain to another, you can perform a redirect like the one below. Simply replace the old and new domains with yours.

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

  • Force the www. version of your domain to be used: Search engines sometimes treat the www and non-www versions of your website as separate sites altogether, which goes against most search engine optimization (SEO) practices. You can, however, choose your preferred version of the website, so all visitor traffic is forced to www.yourdomain.com instead of just yourdomain.com.

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

  • Force the non www version of the domain to be used: Similarly to the above redirect, you can also choose to force all website traffic to the non-www version of your website, by using the example below.

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

After uploading your updated .htaccess file to the web server, you will need to synchronize the changes for them to take effect. This is done via the customer control panel. Instructions on synchronizing the .htaccess file to your web server can be found here: How to Sync an Htaccess File to the Web Server

If you have any further questions that were not answered in this article, please contact us.

(5 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Copyright © 2016 HostMySite