Redirect SSL with .htaccess

If you use an SSL certificate to secure pages on your website, you may want to setup your server to always display non-secure pages using ‘http’ instead of ‘https’

In terms of search engine optimisation it is advisable that your server is setup to display a single version of a page to avoid duplication.
In other words, when an SSL certificate is setup, it generally means that every page on the site is available at http://www.yoursitedomain.com and https://www.yoursitedomain.com.
Other than duplicate content, this can also cause the splitting of link equity between two versions of the same page.
This can happen when a user navigates to a secure page on your site (‘https’) and then navigates to other areas of the site whilst still remaining on the secure version of the site (which is the default behaviour on most Apache servers with SSL installed).
The rest of this blog psot explains how to handle SSL redirection using at .htaccess file installed on your server.
How to Create .htaccess Files
To create .htaccess files you will need to use a simple text editor such as Notepad on Windows or TextEdit on an Apple Mac. The .htaccess file should be saved with no file extension and it is important to include the full stop ‘.’ at the beginning of the filename. To do this, click Save As, and name the file .htaccess. If the program tries to save the file as .rtf or .txt, change this option to All Files.
Redirect Folder to HTTPS
Create the following .htaccess file and place it within the folder that you want to serve using SSL. Change the domain name and folder name to match your sites requirements. The .htaccess file will redirect http://www.yousitedomain.com/foldername/ to https://www.yoursitedomain.com/foldername/, it will also redirect any other pages within the folder to the ‘https’ version.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{REQUEST_URI} foldername
RewriteRule ^(.*)$ https://www.yoursitedomain.com/foldername/$1 [R=301,L]
Redirect Non-Secure Pages to HTTP
The following .htaccess file needs to be placed in the websites root folder. Change the domain name to match your sites requirements.
RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://www.yousitedomain.com/$1 [R=301,L]
Please Note: When making adjustments to your server settings, please test the above method thorougly to check that it works correctly in all situations. Server’s can be setup with various different configurations. If this is the case the above .htaccess files may not work in their current format. It may be a simple case of changing the port settings in the .htaccess files or you may need to contact your website host for more information.
Continue Reading

Creating 301 Redirects

PHP Redirect:

<? Header( “HTTP/1.1 301 Moved Permanently” ); Header( “Location: http://www.new-domain.com” ); ?>

In ASP:

Open the old web page and replace all its content with the following code, replacing “www.new-location.com” with the file name of the new page.

<%@ Language=VBScript %> <% Response.Status=”301 Moved Permanently” Response.AddHeader “Location”, “http://www.new-location.com” %>

In ASP .NET:

<script runat=”server”> private void Page_Load(object sender, System.EventArgs e) { Response.Status = “301 Moved Permanently”; Response.AddHeader(“Location”,”http://www.new-location.com”); } </script>

In IIS (on a Windows server):

1. In internet services manager, right click on the file or folder you wish to redirect. 2. Select “a redirection to a URL”. 3. Enter the redirection page. 4. Check “The exact url entered above”, and the “A permanent redirection for this resource”. 5. Click “Apply”.

web.config 301 redirect

The first example will redirect single pages to a new location. For example, important pages of your site have .htm extensions and you want the new location to be its own directory (IE. http://domain.com/services.htm will change to http://domain.com/services/).

1. Open web.config in the directory where the old pages reside 2. Then add code for the old location path and new destination as follows:

<configuration>
  <location path="services.htm">
    <system.webServer>
      <httpRedirect enabled="true" destination="http://domain.com/services" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
  <location path="products.htm">
    <system.webServer>
      <httpRedirect enabled="true" destination="http://domain.com/products" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
</configuration>

You may add as many location paths as necessary.

The second example will redirect an entire directory to a new location. For example, if you want http://domain.com/olddir/ redirected to http://domain.com/newdir/ open web.config in /olddir and add the following line of code within the <system.webServer> section:

<httpRedirect enabled="true" destination="http://domain.com/newdir" httpResponseStatus="Permanent" />

Canonical Issues – Redirecting non-www to www:

Some webmasters prefer to redirect domain.com to www.domain.com for search engine optimization purposes. The thought is that some incoming links point to their non-www domain and some point to the www domain. So if the domains are consolidated, incoming links are as well. Webmasters who use this redirect usually see instances of search engine rankings with and without the www in their listings and want to consolidate their efforts.

Canonical Redirect using IIS 7.0

For this to work, you need to download and enable the URL Rewrite module for IIS 7. Then the following code is used in the ASP.NET web.config file:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain.com$" />
          </conditions>
          <action type="Redirect" url="http://www.domain.com/{R:0}"                redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

 

Continue Reading