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

Time:01-13

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?

CodePudding user response:

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.

CodePudding user response:

http://example.com/index.html 301 redirects to https://example.com/index.html then repeatedly 301 redirects to https://onebanquethall.com until it times out.

This shouldn't happen on Apache. (Are you on a LiteSpeed server?) This would be an issue if you were trying to redirect from index.php (since you later rewrite the request to index.php), but index.html should be OK, even it is an assigned DirectoryIndex document.

You might be able to resolve this by simply resetting the DirectoryIndex at the top of your script to explicitly remove index.html. For example:

DirectoryIndex index.php

You can also add a condition to that rule so that it only redirects the initial request from the client and not any internal rewrites/subrequests that may be occurring.

For example, the complete code would look like:

DirectoryIndex index.php

RewriteEngine On

# Specific redirects
RewriteCond %{THE_REQUEST} ^GET\s/index\.html
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]

# Redirect HTTP to HTTPS (everything else)
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# BEGIN WordPress
: etc.

The RewriteCond (condition) directive that checks against THE_REQUEST server variable ensures it will only redirect direct requests from the client. THE_REQUEST contains the first line of the HTTP request headers and does not change if the request is rewritten. The RewriteCond directive only applies to the first RewriteRule directive that follows.

The "specific" redirects should go before the general HTTP to HTTPS in order to avoid a double redirect when requesting the old URLs over HTTP (which is what you are testing).

  •  Tags:  
  • Related