How to Configure Apache Redirects with .htaccess Files

Configuring redirects in Apache using .htaccess files is a common way to manage URL changes, handle SEO needs, and improve user experience. The .htaccess file allows you to configure web server settings on a per-directory basis, making it easy to create redirects without altering the main server con

Last updated 30 Nov 2026Also in: Dedicated Servers
Configuring redirects in Apache using .htaccess files is a common way to manage URL changes, handle SEO needs, and improve user experience. The .htaccess file allows you to configure web server settings on a per-directory basis, making it easy to create redirects without altering the main server configuration.

Hereโ€™s a guide on how to set up various types of redirects using .htaccess files.

Step 1: Locate or Create the .htaccess File

  • Navigate to the root directory of your website, typically /var/www/html or similar.
  • If a .htaccess file doesnโ€™t exist, create one:
touch .htaccess
  • Ensure the file has the correct permissions, so itโ€™s readable by the web server:
chmod 644 .htaccess

Step 2: Enable .htaccess Usage in Apache

Before .htaccess files can be used, ensure that your Apache configuration allows it. Open the Apache configuration file (usually in /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf), and make sure the AllowOverride directive is set to All for the directory where you want .htaccess to work.

Example configuration:

<Directory /var/www/html>
    AllowOverride All
</Directory>

After editing, restart Apache:

sudo systemctl restart apache2   # Ubuntu/Debian
sudo systemctl restart httpd     # CentOS/RHEL

Step 3: Configure Redirects in .htaccess

There are various types of redirects you can set up with .htaccess. Here are the most commonly used ones:

1. Simple Redirects (301 Permanent and 302 Temporary Redirects)

301 Redirect (Permanent): A 301 redirect tells search engines that the URL has permanently moved.

Redirect 301 /old-page https://example.com/new-page

302 Redirect (Temporary): A 302 redirect indicates the change is temporary.

Redirect 302 /old-page https://example.com/new-page

2. Redirect All Traffic to HTTPS

  • If you want to redirect all traffic from HTTP to HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

3. Redirect a Domain to Another Domain

To redirect an entire domain to another domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldexample\.com$ [NC]
RewriteRule ^(.*)$ https://newexample.com/$1 [L,R=301]

4. Redirect a Specific Page

To redirect only a specific page, use the following syntax:

Redirect 301 /old-page.html /new-page.html

This assumes that both pages are on the same domain. For different domains, specify the full URL of the new page.

5. Redirect a Directory to Another Directory

If you want to redirect an entire directory, such as /old-directory, to a new location, use:

Redirect 301 /old-directory /new-directory

Or, if the directory is on another domain:

Redirect 301 /old-directory https://example.com/new-directory

Step 4: Test the Redirects

  1. Clear your browser cache to ensure the changes are loaded.
  2. Navigate to the old URLs to confirm that they redirect as expected.

Example .htaccess File

# Enable Rewrite Engine
RewriteEngine On

# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect old page to new page
Redirect 301 /old-page https://example.com/new-page

# Redirect entire domain
RewriteCond %{HTTP_HOST} ^oldexample\.com$ [NC]
RewriteRule ^(.*)$ https://newexample.com/$1 [L,R=301]

# Redirect directory
Redirect 301 /old-directory https://example.com/new-directory
cloud servers in kenyaservers