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
?
2
Answers
You are using
Redirect
withRewriteRule
. These are two different Apache modules and work differently if intermixed. You can use the followingRewriteRule
based redirectionMake sure to clear your browser cached data before testing these updated htaccess rules.
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 toindex.php
), butindex.html
should be OK, even it is an assignedDirectoryIndex
document.You might be able to resolve this by simply resetting the
DirectoryIndex
at the top of your script to explicitly removeindex.html
. For example: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:
The
RewriteCond
(condition) directive that checks againstTHE_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. TheRewriteCond
directive only applies to the firstRewriteRule
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).