Change Primary website or Redirect everything except admin panel from primary to www version

Keywords: WordPress Multisite - Google Cloud Platform - Technical issue - Application configuration

bnsupport ID: 8d60204d-8fed-4ffc-c4dd-9f510f62ea77

bndiagnostic output:

? Apache: Found possible issues
? Resources: Found possible issues
? Mysql: Found possible issues
https://docs.bitnami.com/general/apps/wordpress/administration/use-pagespeed/#disable-pagespeed
https://docs.bitnami.com/general/apps/wordpress/troubleshooting/debug-errors-apache/
https://docs.bitnami.com/installer/faq/linux-faq/administration/increase-memory-linux/

bndiagnostic failure reason: The suggested guides are not related with my issue

Description:
I was flagged for using more then two links that why I had to use (dot).

I used a non www to be my primary website. However, I only want to use ww(dot)example(dot)com to be shown.

This is not true for every website on my multisite as there are sites with example2(dot)com and not www.example2.com.

So I came to the idea that I will need to either change my Main Websites to www(dot)example(dot)com or redirect all traffic from example(dot)com THAT is not needed by admin such as
/wp-admin/
/wp-json/
/wp-login/

to the www(dot)example(dot)com

I already tried to do this on bitnami.conf with the following code

# Default Virtual Host configuration.

<IfVersion < 2.3 >
  NameVirtualHost *:80
  NameVirtualHost *:443
</IfVersion>

<VirtualHost _default_:80>
  DocumentRoot "/opt/bitnami/apache2/htdocs"
RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)
    RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  <Directory "/opt/bitnami/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    <IfVersion < 2.3 >
      Order allow,deny                          
      Allow from all
    </IfVersion>
    <IfVersion >= 2.3 >
      Require all granted
    </IfVersion>
  </Directory>

  # Error Documents
  ErrorDocument 503 /503.html

  # Bitnami applications installed with a prefix URL (default)
  Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf"
</VirtualHost>

# Default SSL Virtual Host configuration.

<IfModule !ssl_module>
  LoadModule ssl_module modules/mod_ssl.so
</IfModule>

Listen 443
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !EDH !RC4"
SSLPassPhraseDialog  builtin
SSLSessionCache "shmcb:/opt/bitnami/apache2/logs/ssl_scache(512000)"
SSLSessionCacheTimeout  300

<VirtualHost _default_:443>
  DocumentRoot "/opt/bitnami/apache2/htdocs"
  SSLEngine on
SSLCertificateFile "( Substracted )"
SSLCertificateKeyFile " ( subtracted )"
RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HREQUEST_URI} !*\/wp-admin\/ [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]            
  <Directory "/opt/bitnami/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride All
    <IfVersion < 2.3 >
      Order allow,deny                          
      Allow from all
    </IfVersion>
    <IfVersion >= 2.3 >
      Require all granted
    </IfVersion>
  </Directory>

  # Error Documents
  ErrorDocument 503 /503.html
        
  # Bitnami applications installed with a prefix URL (default)
  Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf"
</VirtualHost>

# Bitnami applications that uses virtual host configuration
Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-vhosts.conf"

But I believe this affects all websites and there is a syntax error on line 57 due to bad regex.

Questions:

  1. What do you believe is the best solution to make sure only www.example(dot)com is shown to the user while not affecting the rest of the system.
  2. Where can I add htaccess rewriteEngine commands to change ONLY example(dot)com and not foo.com

Final notes
Thank you for your servers that are a lifesaver I have been trying to solve this issue on my own for over three weeks. If you could point me in the right direction it would be a lifesaver!

Best regards,
Adam

Hi @avacadoadam,

Thanks for using Bitnami. Unfortunately I don’t know if WordPress allows the mixed configuration you are trying to achieve or not. I’d say that if you configure your primary domain as www.example.com all the internal URLs will be generated with the www subdomain, including the admin panel.

On the other hand, if you configure Apache to redirect example.com to www.example.com but WordPress internally uses example.com, I think you will enter a redirection loop that will lead to errors accessing your website.

We recommend you to open a new question in the official WordPress forums to get more information from the app developers on this and how to achieve that (if supported by the application).

https://wordpress.org/support/forums/

Apart from that, notice that your Apache configuration will never reach to the www redirection you added, as the HTTPS redirection will take place first and it will using the configuration for the virtualhost 443 from that point. However, I think it is a good idea to first clarify if using mixed URLs is possible or not, and how to achieve it, with the WordPress support team.

Hey, @gongomgra Thanks for the quick response!

I am not the same concerned with redirecting and not getting all the possible variations of the internal links seems messy.

What do you think about changing the primary websites?
One would think it should be relatively easy.
But I am looking up tutorials with completely differing approaches.

Tried and failed.

Did you ever encounter somebody who change their primary website from example.com to www.example.com?
If so do you have a good tutorial perhaps with accurate steps to produce this result?

5 stars for the Bitnami support team!

Best regards,
Adam

Hi @avacadoadam,

This is a very specific configuration for your site. For example, we suggest you use the www domain or the non-www one for all the domains you have. That means that if you want to use www for one site, you should also use www for the other sites for consistency. You can configure all the sites by following this guide

https://docs.bitnami.com/google/apps/wordpress-multisite/configuration/configure-wordpress-multisite/

You can also redirect to the www domain or the non-www one by following this other guide in our docs

https://docs.bitnami.com/google/apps/wordpress-multisite/administration/redirect-custom-domains/#add-redirection-rules-124

Please note that you do not need to modify the httpd-vhosts.conf file but the bitnami.conf one under the apache2/conf folder.

However, if you want to use www for some sites and non-www domains for the other ones, you will need to custom the Apache’s configuration to look similar to this

...
  RewriteCond %{HTTP_HOST} ^www\.nonwwwdomain\.com$ [NC]
  RewriteRule ^(.*)$ http://nonwwwdomain.com$1 [R=permanent,L]
  RewriteCond %{HTTP_HOST} ^wwwdomain\.com$ [NC]
  RewriteRule ^(.*)$ http://www.wwwdomain.com$1 [R=permanent,L]
...

Happy to help!


Was my answer helpful? Click on :heart:

@jota I wish I knew it would that complicated to host www and non www.

However, I found a nice solution to the problem. For anybody facing similar issues.

First, install 10up/MU-Migration for WordPress CLI.

Export all the websites you would like to change to primary using the ID param on the CLI.

Then reimport it with the ID of 1 or the primary website.

Then ensure to change wp-config.php the URL of the primary website.

And boom www.cryt***.ie is up and running and cryt**.ie redirect properly.

Couldn’t be happy, Thank you guys for the support both @jota and @gongomgra
I could create a short video on how to do this or a more detailed example if anybody needs it in the future.

Keep on hacking guys!

EDIT: @gongomgra removed public link to avoid spam

Hi @avacadoadam,

Thanks for the info. I’m glad you fixed your issue! We will close this thread as solved. Please do not hesitate to open a new one with any other questions you may have.