How can I do a 301 redirect of index.html and force HTTPS?

I'm moving an old html site to Wordpress. The site structure looked like:

  • http://example.com/index.html
  • http://example.com/about.html
  • http://example.com/contact.html

And in WordPress it now looks like:

  • https://example.com/
  • https://example.com/about/
  • https://example.com/contact/

This is my .htaccess file

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    
    Redirect 301 /index.html https://example.com/
    Redirect 301 /about.html https://example.com/about/
    Redirect 301 /contact.html https://example.com/contact/
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

(Edited here to correct the error I'm getting) This works, except http://example.com/index.html 301 redirects to https://example.com/index.html, then repeatedly 301 redirects to https://example.com/ until it times out.

The Redirect 301 /index.html https://example.com/ gets ignored whether I place it where it is, or above the RewriteRule line.

The other redirects that work do a 301 redirect from http://example.com/about.html to https://example.com/about.html then another 301 redirect to https://example.com/about/ (200 OK).

How can I redirect http://example.com/index.html to https://example.com?


Solution 1:

You are using Redirect with RewriteRule . These are two different Apache modules and work differently if intermixed. You can use the following RewriteRule based redirection

RewriteEngine On
RewriteCond %{HTTPS} !=on

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

RewriteRule ^index.html$ https://example.com/ [L,R=301]
RewriteRule ^about.html$ https://example.com/about/ [L,R=301]
RewriteRule ^contact.html$ https://example.com/contact/ [L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Make sure to clear your browser cached data before testing these updated htaccess rules.