Search code examples
htmlwordpressapache.htaccesshttp-redirect

Redirecting nonexistent index.html to home page in Wordpress is causing too many redirects in browser


I recently changed my website from static html to Wordpress, and in doing so I've been redirecting all of my former and nonexistent pages with my .htaccess file whenever Google shows me a crawl error. I've been successful in redirecting all crawl errors until today. My old index.html is throwing a crawl error and when I use:

Redirect 301 /index.html http://www.example.com

... I get this from my browser:

Too many redirects occurred trying to open www.example.com. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.*

I have since removed the above redirect from my .htaccess file and will just live with the crawl error if I can't get this resolved. But I'm thinking somebody knows how to solve this and if so, I'd really appreciate it if you'd let me know how.


Solution

  • You could experience this redirect loop if your DirectoryIndex is set to index.html (as the first option), which is likely to be the default setting on your server.

    Basically, when you access a directory, eg. http://example.com/ (the root directory) then the DirectoryIndex directive tells the server which file to serve (via an internal rewrite). eg. http://example.com/index.html. This then seeds the redirect loop.

    Since you are using WordPress, you could change this in .htaccess to favour index.php instead. For example, at the top of your .htaccess file:

    DirectoryIndex index.php index.html
    

    However, you could also solve this by using mod_rewrite (which is probably preferable). In fact, since WordPress already uses mod_rewrite (eg. RewriteRule directive) then you should also be using mod_rewrite for your redirects, not mod_alias (eg. Redirect directive). You should not mix redirects from both modules in .htaccess since you can easily get conflicts. Different modules execute at different times, regardless of their order in .htaccess.

    By using mod_rewrite you can avoid a redirect loop by checking against THE_REQUEST. For example:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html [NC]
    RewriteRule ^index\.html$ / [R=301,L]