Makers Byte
Published in

Makers Byte

How to do redirect using .htaccess file?

The .htaccess is a distributed configuration file, and is how Apache handles configuration changes on a per-directory basis. The file is used to manipulate how Apache serves files from its root directory, and subdirectories thereof.

This article will explain how to create a .htaccess file to redirect your site or site content.

As a configuration file, .htaccess is very powerful. Hence even the slightest syntax error (like a missing space) can result in your content not displaying correctly or at all.

To start:

  1. Have access to your FTP
  2. Make sure all hidden files are visible

Now, if you don’t have .htaccess already created, create an empty text file using a text editor such as notepad, and save it as htaccess.txt. Now edit the contents of the htaccess.txt file.

Once you have placed in your code, drop the file to the root folder of your site via FTP and rename the file to .htaccess.

Below are some example redirects you can use. They are labeled in accordance to what actions they will perform.

Redirect old domain to new domain

Create a file called .htaccess in the doc root (web / httpdocs / www folder) of old-domain.com and put in the code below:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://new-domain.com/$1 [R=301,L]
The above is an example of the code for the domain transfer from old-domain.com to new-domain.com

Redirect to www

Create a file called .htaccess in the doc root (web / httpdocs / www folder) of mydomain.com and put in the code below

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

The above is an example of the code for the domain transfer from http://mydomain.com to http://www.mydomain.com

Redirect old directory to new directory

Create a file called .htaccess in the doc root (web / httpdocs / www folder) of your domain and put in the code below

RedirectMatch 301 /old-directory/(.*) /new-directory/$1

The above is an example of the code for the domain transfer from http://mydomain.com/old-directory/ to http://www.mydomain.com/new-directory/

Redirect old URL to new URL

Create a file called .htaccess in the doc root (web / httpdocs / www folder) of mydomain.com and put in the code below

Options +FollowSymlinks
RewriteEngine on
rewriterule ^showresults.php(.*)$ http://otherdomain.com/displayresults.aspx$1 [r=301,nc]

The above is an example of the code for the redirect of old url http://mydomain.com/showresults.php to http://otherdomain.com/displayresults.aspx

301 (Permanent) Redirect:

Use a 301 redirect .htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the “example.com” domain.

When adding the following to your website’s .htaccess file, be sure to replace example.com with your own domain name.

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://example.com/

302 (Temporary) Redirect:

Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:

# This allows you to redirect your entire website to any other domain
Redirect 302 / http://example.com/

Redirect index.html to a specific subfolder:

# This allows you to redirect index.html to a specific subfolder
Redirect /index.html http://example.com/newdirectory/

Redirect an old directory to a new directory:

# Redirects example.com/old to example.com/new
RewriteRule ^old/(.*)$ /new/$1 [R=301,NC,L]

Redirect an old file to a new file path:

# Redirect old file path to new file path
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html

Redirect to a subfolder with URL masking:

# Show the content in example.com/folder2, but the URL appears as example.com/folder1
RewriteEngine On
RewriteRule ^folder1/?$ /folder2/
# To show the URL as just example.com
RewriteEngine On
RewriteRule ^/?$ /folder2/

Redirect to a specific index page:

# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html

Redirect an error message:

Instead of prompting a 404 Not Found error page, the site will redirect to the homepage:

# Redirect 404 Error pages to the home page
ErrorDocument 404 http://example.com/

Redirect a non-existing page to index.php

# Redirect non-existing pages to index.php
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

If you need to make complex changes to the way your URL displays, you should visit using .htaccess rewrite rules by apache.

--

--

--

Resource hub for Designers/Developers

Recommended from Medium

The Computer Science Undergraduate Guide To The 2020 Quarantine

Framework Start— and a rise from the ashes.

Spring Boot 001: Set up and Ngrok

The Deutsch-Jozsa Algorithm - Math, Circuits, and Code: Quantum Algorithms Untangled

Quantum Computing, a Guide to the Deutsch-Josza Algorithm and Problem

Scheduling Backups on an Oracle Cloud VPS

Featured

Three Tips for Faster and Higher-Quality Software Development

Savix Token Transparency Sheet

Demo: Trip Builder revisited — a full-stack vacation planning app

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shahzaib Khan

Shahzaib Khan

Data Scientist, Developer, Tech Writer, Entrepreneur and Computer Science Enthusiast. Connect with me @ https://linkedin.com/in/shahzaibkhan/

More from Medium

Getting Started with web development

Contributors in your Github repository

Roadmap to Become a Pro in MERN Stack Development

Control your incoming traffic on your website AWS