301 redirect loop in WordPress

Refresh

March 2019

Views

2.4k time

1

Initially used a 301 to redirect www.example.com/ → www.example.com/wp

Unfortunately, I didn't read all the 'don't use 301 unless your 100% sure it's permanent' and now I need to revert back to the original domain.

At first, I tried to do a regular site url/wordpress url change in Setting/General in the admin dash. Saved over the old .htaccess on the root that had the original 301 redirect. Didn't work.

I moved everything to the root directory because I was getting a 'This webpage is a redirect loop' error page. Cleared cache on all browsers. Still getting the redirect loop error page.

Checked my url redirection here: http://www.digitalcoding.com/tools/url-redirect-check.html

I have two prompts, the first is going through fine, the second is a big fat X in red:
301 Moved Permanently: www.example.com/ → www.example.com
301 Another Redirect Detected: www.example.com

.htaccess looks like this:

#Use PHP 5.4
AddType application/x-httpd-php54 .php
<IfModule mod_suphp.c>
suPHP_ConfigPath /opt/php54/lib/php.ini
</IfModule>
ErrorDocument 401 default
# 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

Any insight?

1 answers

2

Redirect 301 / http://www.example.com/ You get an endless loop because there is nothing in that code to tell it not to redirect www.example.com to itself.

You are correct that you cannot use the Redirect directive, and this is the reason; It is unconditional, and will cause a loop in the scenario you describe.

In order to prevent the loop, you must find a way to tell the code not to redirect www.example.com to itself. This can be done by using mod_rewrite, and specifically, the RewriteCond directive in mod_rewrite, to test the requested hostname and act accordingly:

 Options +FollowSymLinks -MultiViews
   RewriteEngine on
   #
   # if requested hostname is non-blank
   RewriteCond %{HTTP_HOST} .
   # and if requested hostname is NOT "www.example.com"
   RewriteCond %{HTTP_HOST} !^www\.example\.com
   # redirect to same object in correct domain
   RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The first directive, Options, may or may not be required on your server. If it is not required, it may in fact not be allowed. Comment it out of you have trouble.

The second directive, RewriteEngine, is required once (and only once) at the top of your mod_rewrite code.

The third directive, the first RewriteCond, is only required if you do not use a name-based virtual (shared) server. It prevents an infinite loop if the client does not send a "Host" header with its request. Since it is impossible to access a name-based virtual server without a "Host" header, this line is not required on a name-based virtual server. No harm will come from leaving it in, except that it takes a little time to process it.

Note that this code will also redirect "example.com" to "www.example.com", and so serves to canonicalize your main domain name as well, preventing ranking dilution from having duplicate content on two variations of the domain.