Working with the .htaccess and Redirects

URL redirections let web admins forward visitors to the new location when a page moves or gets deleted.  

Web direct helps mitigate against traffic loss to a website and potential sales due to a moved or deleted page. It can also make the navigation experience seamless by preventing visitors from running into No Found errors when visiting their favorite pages.

Web redirects also let web admins force all visitors, including hackers, to connect securely to the website through HTTPS and address duplicate content concerns.

You can place a redirect rule on your domain’s .htaccess file. Let’s find out what it is and how to add different redirect rules.

Let’s dive in.

What’s .htaccess File

The .htaccess file, short for hypertext access file, is a server configuration file. 

It lets users make quick changes to their server settings. However, the file configures the server only for the directory it’s in. So, for instance, the .htaccess file in the root directory (public_html) can only configure server settings for the entire root domain.

If you wish to configure server settings for a subdomain, you’ll need to do that in its .htaccess file. You can also place the file in a subdirectory, for example,/downloads, to configure server settings for it.

As a server configuration file, any error or misconfiguration on the .htaccess will affect your website.

Types of Web Redirects

Web redirection could be permanent or temporary. Let’s examine quickly what each means.

Permanent Redirect

Permanent redirect or 310 redirect indicates permanent relocation.

It helps users forward web visitors to a new page when the previous location moves permanently or gets deleted.

 Use this redirection type if you don’t wish to bring back the old URL.

Temporary Redirect

Temporary redirect or 302 redirect indicates temporary relocation.

It forwards visitors from a page to another temporarily and is helpful if you wish to reinstate the URL later. You could use temporary redirects when carrying out website maintenance.

When you build a new page, you could temporarily redirect your visitors to it when they visit the old page to get their feedback before deleting it.

Creating Different Redirects Rules

You can create web redirects by placing web redirect rules on the .htaccess file using the correct syntax. Let’s explore some of the redirects you can make.

Redirecting the Homepage to a Specific Subfolder

You can redirect permanently to another URL on the same website by adding this code to your .htaccess file.

RewriteEngine On 

Redirect 301 /index.html https://newdomain.com/new/newdirectory

Redirect 301 in the code indicates permanent redirect. You could replace 301 with 302 if you wish to redirect temporarily. 

Please, don’t forget to replace https://newdomain.com/newdirectory with the directory you wish to redirect to. Some websites use /index.php as the index file, so replace /index.html if it applies to your website.

Don’t add the RewriteEngine On line if you already have it at the beginning of your file before the closing </Ifmodule> tag. 

Always begin your .htaccess file with the line.

Working with the .htaccess and Redirects

Redirecting to a New Page

To redirect a page to another URL on the same website:

  • Redirect 301 /old /new

Redirecting to External Pages

To redirect a page to an external destination:

  • Redirect 301 /old www.domain.com/new-location

Redirecting a Website to a New Domain

To redirect your entire website to a new domain or a parked domain name to your main website:

  • Redirect 301 / www.yournewdomain.com/

Redirecting Old Directory a New Directory

To redirect a directory to a new director (for instance, www.yourdomain.com/old to www.yourdomain.com/new):

  • RewriteRule ^old/(.*)$ /new/$1 [R=301,NC,L]

Redirecting With Masking

To mask the URL of the new location (for instance, redirecting to the subfolder www.domain.com/folder but wants to show its URL in the address bar as www.domain.com/files) :

  • RewriteRule ^files/?$ /folder/

Show only the domain name without the file path (for example, domain.com):

  • RewriteRule ^/?$ /folder/

Redirecting to a Specific Index Page

To set a default index page (for example, index.html instead of index.php):

  • DirectoryIndex index.html

Redirecting 404 Pages to the Homepage

To redirect the 404 error page to the homepage to prevent visitors from running into No Found errors:

  • ErrorDocument 404 https://domain.com/

Redirecting Non-Existing Page to the Homepage

To redirect visitors to the homepage when they enter non-existing pages – 

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

HTTPS Redirection

To force all visitors to connect via HTTPS – 

RewriteCond %{HTTPS} off 

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Non-www Redirection

To redirect a non-www URLs to www

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

To redirect www URLs to non-www

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Temporary Redirect

To redirect a page to a new location temporarily:

  • Redirect 302 /photo /new-location

To temporarily redirect an entire website:

  • Redirect 301 /photo www.domain.com

Modifying the .htaccess File

To create a redirect, you’ll need to edit your domain’s .htaccess file to add the redirect rule, and here’s how to go about it.

But first, follow these steps to create a .htaccess file if you don’t have it already.

Step 1: Sign in to Your SPanel Account

Log in to SPanel account — Scala Hosting all-in-one control panel. Visit www.domain.com/spanel/login, and enter your email (or username) and password to log in.

Remember to replace domain.com with your domain name.

Working with the .htaccess and Redirects

If you sign in as a super admin, Spanel takes you to the admin dashboard but takes you straight to your account’s control panel if you log in as a user.

Step 2: Choose your Account 

To access your account’s control panel from the admin dashboard, click List Accounts under the QUICK LINKS section.

Working with the .htaccess and Redirects

Please scroll to the account you wish to log into its control panel.

Working with the .htaccess and Redirects

Now, click Actions > Login to access the account’s control panel.

Working with the .htaccess and Redirects

Step 3: Open File Manager

Locate the FILES section on the control panel and click File manager.

Working with the .htaccess and Redirects

You’ll see all your domains’ root directories when you open the file manager. Please scroll to the public_html folder to open your root domain’s root directory.

Working with the .htaccess and Redirects

But if you wish to create the redirect rule for a subdomain, choose the subdomain’s specific folder.

Step 3: Create the File

In the domain’s root directory, click the New File/Folder icon and select New Files.

Working with the .htaccess and Redirects

Create a new file named .htaccess and save.

Working with the .htaccess and Redirects

Step 4: Add Your Code

Add these lines to your newly created .htaccess file before entering your redirect rules, as explained earlier –

<IfModule mod_rewrite.c>

RewriteEngine On

Now, copy and paste all your redirect codes below the RewriteEngine On line, then add this closing tag.

</IfModule>

Working with the .htaccess and Redirects

But if you have an existing .htaccess file, locate it in your root directory, and right-click the file to reveal a menu; choose Edit to open the editor.

Working with the .htaccess and Redirects

Copy and paste your web redirect code into the editor and save. Then, you could visit the redirected page to confirm that it’s forwarding traffic to the specified location.

Need Support?

If you need assistance creating any redirect for any specific purpose, kindly contact our support for quick help, and we’ll be available to help.

Rado

Author

Working in the web hosting industry for over 13 years, Rado has inevitably got some insight into the industry. A digital marketer by education, Rado is always putting himself in the client's shoes, trying to see what's best for THEM first. A man of the fine detail, you can often find him spending 10+ minutes wondering over a missing comma or slightly skewed design.

Write a Comment

Required*