1. Home
  2. Hosting Management
  3. cPanel
  4. Domains
  5. .htaccess 101 – Rewrite and Redirection

.htaccess 101 – Rewrite and Redirection

This article will explain how to redirect your domain using a .htaccess file using common redirect rules.

.htaccess is a configuration file for use on web servers running the Apache Web Server software.

Since .htaccess is a hidden system file, please make sure your FTP client is configured to show hidden files. This is usually an option in the program’s preferences/options.

Note

We’ll assume that you have mod_rewrite installed and enabled on your server.

How to Redirect HTTP Requests to HTTPS

In order to redirect your website to be opened through HTTPS, you should add the following rewrite rule to your .htaccess file:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.domain.com%{REQUEST_URI} [R,L]

This will redirect your domain to https://www.domain.com

If you wish the redirect to work without www, you should remove it from the rewrite rule:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://domain.com%{REQUEST_URI} [R,L]

This will redirect your domain to https://domain.com

How to Redirect an Old Domain to a New Domain

In order to redirect your old domain to a new domain name, you should add the following rewrite rule to your .htaccess file:

RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.new-domain.com/$1 [R=301,L][/php]

How to Redirect From a Non-Www to Www URL

In order to redirect your site from a non-www to www URL, you should add the following rewrite rule to your .htaccess file:

RewriteEngine on
RewriteBase /
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

How to Redirect a Domain’s Page to a Different Page

In order to redirect a specific page or section of your site to a different one, you should add the following rewrite rule to your .htaccess file:

Redirect 301 /page-name http://domain.com/new-page

How to Redirect an Entire Site to a Subfolder

In order to redirect your entire and for it to load from a subfolder, you should add the following rewrite rule to your .htaccess file:

Redirect 301 / http://domain.com/subfolder-name/

How to Redirect a Subfolder to a Different Domain

In order to redirect a subfolder to a different site, you should add the following rewrite rule to your .htaccess file:

Redirect 301 /subfolder-name http://different-domain.com/

How to Redirect All Files with a Certain Extension but Retain the File Name

If you want a .html extension to use the same filename but use the .php extension, you should add the following rewrite rule to your .htaccess file:

RedirectMatch 301 (.*)\.html$ http://domain.com$1.php

How to Redirect One Directory to Another

In order to redirect one directory or subfolder to a different one, you should add the following rewrite rule to your .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)/old-directory/(.*)$ $1/new-directory/$2 [R,L]

How to Redirect from a Blog Subdomain to a Blog Folder

In order to redirect blog.oldsite.com to www.newsite.com/blog/ you should add the following rewrite rule to your .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI}/ blog
RewriteRule ^(.*) http://www.new-domain.com/%{REQUEST_URI} [R=302,NC]
RewriteRule ^(.*) http://www.new-domain.com/blog/%{REQUEST_URI} [R=302,NC]

Alias “Clean” URLs

The following snippet lets you use “clean” URLs — those without an HTML extension, e.g. example.com/users instead of example.com/users.html.

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.html [NC,L]

Other Useful .Htaccess Redirects with Examples

Rewrite and redirect URLs with query parameters (files placed in the root directory)

Original URL: http://www.yourdomain.tld/index.php?id=1
Desired destination URL: http://www.yourdomain.tld/path-to-new-location/

You should add the following rewrite rule to your .htaccess file:

RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^index\.php$ /path-to-new-location/? [L,R=301]
Redirect URLs with query parameters (files placed in subdirectory)

Redirect URLs with query parameters (files placed in a subdirectory)

Original URL: http://www.yourdomain.tld/sub-directory/index.php?id=1
Desired destination URL:  http://www.yourdomain.tld/path-to-new-location/

You should add the following rewrite rule to your .htaccess file:

RewriteEngine on
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^sub-directory/index\.php$ /path-to-new-location/? [L,R=301]
Redirect one clean URL to a new clean URL

That’s a wrap!

Remember that ChemiCloud’s infrastructure supports Apache rewrite rules out of the box! If you’d like to experience top speed and reliability and have access to our amazing Support Team, try our Web hosting!

Updated on January 14, 2021

Was this article helpful?

Related Articles

TRY CHEMICLOUD RISK-FREE
Fast, secure cloud hosting. 18 global data centers. Unhappy with your web host?
👉 Migrate for Free

Comments

  1. The below code will set the default page your website shows to whatever you specify. For example, the below code will set your website to show the index.html file as default.

    DirectoryIndex index.html

    However, as I have checked on your site, it’s already picking up the index.html file. So there’s no need for you to do anything else. While SEO isn’t my area of expertise, you shouldn’t experience any issues, as there’s no duplicate content.

  2. One lengthy question. My software names the home page index.html. Is there a code to place in the .htaccess file to designate one page only as the home page? Thus if anyone enters: http://www.eclectic-ware.com or http://www.eclectic-ware.com/index or http://www.eclectic-ware.com/index.html how do I make it clear to the Almighty Google and others that the page is index.html? They all work, but do I have a duplicate content issue without the proper code in the .htaccess file?

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.