Help Docs Performance Website Optimization Redirecting URLs Using htaccess

Redirecting URLs Using htaccess

Ease domain changes and maintain visitor clarity with URL redirects using .htaccess files in your web folder. Keep existing settings; consult a developer if needed.

Editing your htaccess files are an easy way to set up URL redirects If you decide to change domain names or change the organization of your site.   Setting up URL redirects for these examples help make sure your website visitors aren’t confused.

Inside your web site’s public_html folder is a hidden file commonly named Htaccess. If your htaccess file already contains important settings for your website, you can simply make a backup copy and then add the new code to what is already there. Do not delete the existing htaccess file unless it is empty or you are absolutely sure it is okay to delete. Please don’t hesitate to contact our Support team if you’re having trouble with htaccess settings. Some customized settings might require your web developer, but we can help with many htaccess questions.

You will need to SSH into your server account and modify the file using your favorite command line text editor to edit the file. Or, you can also re-create the file yourself and upload it to the server using FTP/SFTP or SCP. This tutorial assumes you have good working knowledge of the htaccess command line and your server’s filesystem and will show you how to format different configuration changes. Again, before making any changes to configuration files, we strongly recommend you take a backup of the file. Once you’ve decided what you want to add to your htaccess file, follow these simple steps:

  1. Log into your server via SSH.
  2. Using the text editor of your choice, open your htaccess file. It will usually be located in your public_html folder. If you don’t have an htaccess file, create a file called .htaccess to hold your configurations.
  3. Add these lines to your file:
    # enable basic rewriting
    RewriteEngine on

    to make sure your URLs can be rewritten.

  4. Then, copy and paste your new configurations into the htaccess file and save the file.
  5. Test your work by going to your website and viewing the pages you wanted to redirect.

Redirecting Webpages and Whole Websites

You use 301 and 302 redirects to redirect individual pages on your website.  A 301 redirect is a permanent redirect, and a 302 redirect is a temporary redirect. These redirects tell the server to send traffic to the new location and also tell website visitors and search engines where the content has moved. This post uses the domain mysite.com as an example only. Please use actual existing domains to suit your requirements.

Redirect 301 /old.html http://www.mysite.com/new.html

Replace /old.html with the directory path of your old page and “http://www.mysite.com/new.html” with the URL of your new page. This will send any visitors who want to access mysite.com/old.html to the page mysite.com/new.html.

You can also redirect a whole website this way.

Redirect 301 / http://www.mysite.com/

This is often used to change an existing domain to a new domain name.

URL Rewrites

Similar to redirects, rewrites also point one target URL to another URL, but this is actually converted by the web server while handling the traffic. A good analogy is like using a pencil and eraser to change a name, as opposed to using a sign to point to the new name.

The most common URL rewrites force sites to have URLs either with or without “www.”

Forcing Non-www Site Addresses

When you add this information to your htaccess file, any visitors who type in “www.mysite.com” will be sent to “mysite.com.”

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^mysite.com
RewriteRule (.*) http://mysite.com/$1 [R=301,L]

Replace “mysite” with your domain. If you have a different top-level domain (e.g., .net or .org instead of .com), use that top-level domain instead of “.com.” Don’t remove the “” as it is needed for the correct syntax.

Forcing www Site Addresses

When you add this information to your htaccess file, any visitors who type in “mysite.com” will be sent to “www.mysite.com.”

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

Replace “mysite” with your domain. If you have a different top-level domain (e.g., .net or .org instead of .com), use that top-level domain instead of “.com.” Don’t remove the “” as it is needed for the correct syntax.

Was this article helpful?