1

I have a WordPress multisite setup with subdomains. I have pointed five subdomains to it. I have also installed SSL to main domain and the other subdomains.

I have written the below Redirect code for redirecting the non-www main domain to www, and all (main domain+sub-domains) to https.

RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Till then it is fine.

But, when I am going to any 404 urls like https://www.example.com/any-404-page, it is redirecting me to https://www.example.com/www.example.com and it is returning ERR_TOO_MANY_REDIRECTS

Is it happening because of my .htaccess code?

  • Did you write those rules yourself? Why do you us http%1 rather than https in the last line? It looks like you are trying to capture the s from the second line, but I don't see how that is going to work because there are also capturing groups on the first line. If you always what https why wouldn't you just specify that rather than trying to use capturing groups? – Stephen Ostermiller Aug 9 '18 at 10:51
  • @StephenOstermiller "but I don't see how that is going to work because there are also capturing groups on the first line" - that regex is a bit confusing. However, the %1 refers to the last matched CondPattern only and the condition %{HTTPS}s ^on(s)| is always successful. The trailing | (alternation) is key and effectively matches anything - although nothing is actually "matched" - the expression is successful. For http, the %1 backreference is simply not defined and results in an empty string. However, as you suggest, there are other issues with those directives. – MrWhite Aug 9 '18 at 19:00
1

I have written the below Redirect code for redirecting the non-www main domain to www, and all (main domain+sub-domains) to https.

RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This is possibly an aside to your immediate problem, however, this code does not do what you say it does. In fact, as it stands, this code does nothing at all for subdomains, as you appear to suggest!?

Specifically...

RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]

This condition will never be successful for any subdomains or a fully qualified main domain (all valid hostnames, apart from the main domain in the case of single-level TLDs, will have at least 2 dot segments), so the following RewriteRule will never get processed.

RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This will always redirect HTTP to HTTP and HTTPS to HTTPS, it does not redirect HTTP to HTTPS. If you need that then you should hardcode the s the the substitution, as @Stephen suggested in comments.

1

It was because of NOBLOGREDIRECT constant in WordPress. I had defined the NOBLOGREDIRECT as www.example.com, so if that URL does not exist, then it redirects to https://www.example.com/www.example.com.

I have changed NOBLOGREDIRECT from www.example.com to https://www.example.com and now it is redirecting to home page.

  • I wouldn't use a rule to redirect non-existent pages to the home page. It isn't good for users. Users benefit from specific redirects to deep pages when there is an change in URL or an obvious mis-typing of the URL. Search engine bots expect to find 404 errors. They view it as a sign of a healthy site that non-existent pages return the correct error code (404). Google treats redirects to the home page as "soft 404" errors and those redirects will appear in your Google Search Console error reports. – Stephen Ostermiller Aug 10 '18 at 14:29
  • That is a bug or feature of Wordpress multisite. There are some other action hooks & filter which we can use to fix it. – Aftab Aug 11 '18 at 15:43

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.