❤️ AZDIGI has officially updated to a new blog system. However, some posts may have incorrect or mismatched images. Please click the Report article button at the bottom of the post so AZDIGI can update as quickly as possible. Thank you!

If you are trying to redirect one or more of your website’s URLs, this article about redirecting with .htaccess will be very helpful.

Note: If you use WordPress, some redirects won’t work because it will use its guidelines by default. And the solution for WordPress is that you should use the Redirection plugin.

I. What is a .htaccess file?

.htaccess is a configuration file for use on supported Web Servers such as Apache, LiteSpeed, OpenLiteSpeed…

When the .htaccess file is placed in a website source directory, the .htaccess file is executed by the Web Server. These .htaccess files can be used to change the configuration of the Web Server software to enable/disable additional functionality and features that the Web Server software provides.

II. Aggregate redirects with .htaccess file

The examples below can be added to the file .htaccess depending on your needs. Just be sure to adjust the actual path to the file, so the .htaccess file knows where it is. In addition, if you see the domain example.com , change this to your domain.

1. Redirect all URLs

The following configuration line will redirect all the URLs on your current site to the new site:

AZDIGI Tutorial
Redirect 301 / https://example.com/
    

2. Redirect to a URL

Using Redirect in a .htaccess file allows you to redirect user traffic from the old site to the new one without keeping the old site.

Example: If you use index.html as your index file and then rename index.html to home.html, you can set up a redirect to take the user from index.html to home.html with the fragment following code:

AZDIGI Tutorial
Redirect /path/to/old/file/index.html /path/to/new/file/home.html
    

If you want to redirect to another website’s home.html file then use this code:

AZDIGI Tutorial
Redirect /path/to/old/file/index.html https://www.example.com/new/file/home.html
    

3. Some examples of redirects using .htaccess

Redirect from a directory to an HTML file:

AZDIGI Tutorial
RedirectMatch 301 ^/blog/about /blog/about.html
    

Redirect the index.html file to another directory:

AZDIGI Tutorial
Redirect /index.html /new/
    

Redirect from index.html file to default.html file:

AZDIGI Tutorial
Redirect /index.html /default.html
    

Redirect an internal /private directory to another site’s /private directory:

AZDIGI Tutorial
Redirect /private/ https://www.example.com/private/
    

Load a .gif file from another site:

AZDIGI Tutorial
Redirect /img/logo.gif https://www.example.com/images/logo.gif
    

Redirect 404 error messages back to website homepage:

AZDIGI Tutorial
ErrorDocument 404 https://example.com/
    

Redirect an old directory to a new directory:

Redirect files in the old directory /blog/archives to a new directory /archives.

AZDIGI Tutorial
RewriteRule ^blog/archives/(.*)$ /newarchives/$1 [R=301,NC,L]
    

4. Redirect non-existent pages to index.php

If a visitor tries to access a page that doesn’t exist, they’ll get a 404 error. Instead, you can redirect any request to a non-existent page to your index.php file (or whatever any other index files) by adding the following code to your .htaccess:

AZDIGI Tutorial
Options +SymLinksIfOwnerMatch 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
    

If your index page is not index.php but index.html or another, just change the last line to your actual index file. The visitor is then redirected back to your homepage.

5. Redirect the main directory to a subdirectory

For example, redirect the URL of the main directory to any subdirectories. In this example, it automatically loads example.com/subdir1 when you type example.com in the browser:

AZDIGI Tutorial
RewriteEngine on
RewriteRule ^$ /subdir1/ [L]
    

6. Redirect to www

AZDIGI Tutorial
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
    

7. Remove www in URL

AZDIGI Tutorial
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) https://example.com/$1 [R=301,L]
    

8. Rewrite one URL to another

This example rewrites one URL to another. It will rewrite example.com/1.html to example.com/abc.php?id=1 .

AZDIGI Tutorial
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+).html /abc.php?id=$1 [QSA,L]
    

9. Rewrite non-existent links back to the homepage

Redirect all links to non-existent files or directories to index.php. However, if the file or path does exist, it will be accessible normally:

AZDIGI Tutorial
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

10. Redirect http to https

AZDIGI Tutorial
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUESTURI} [R=301,L]

III. Summary

AZDIGI hopes these .htaccess redirect examples will help you to solve your problems. Wishing you success!

See more useful website tutorial articles at the following link:

If you need assistance, you can contact support in the ways below:

  • Hotline 247: 028 888 24768 (Ext 0)
  • Ticket/Email: You use the email to register for the service and send it directly to: support@azdigi.com .
  • Click on the AZDIGI website to refer to the best hosting/VPS service in Vietnam.
Share:
This article has been reviewed by AZDIGI Team

About the author

Thạch Phạm

Thạch Phạm

Đồng sáng lập và Giám đốc điều hành của AZDIGI. Có hơn 15 năm kinh nghiệm trong phổ biến kiến thức liên quan đến WordPress tại thachpham.com, phát triển website và phát triển hệ thống.

10+ years serving 80,000+ customers

Start your web project with AZDIGI