htaccess Q: 80% good, now need to target primary domain only...

Hi WPMUDev,

I hope your day is going well :slight_smile:

I am working with a subdomain multisite and am working on some url cannonicalization details.

I have an htaccess Q for you...

This is working with a bit of code drawn from PerishablePress's htaccess book's resources for WordPress.

The base code is as follows:

# BEGIN Remove www
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
#END Remove www

This works for my very well as-is, except when using Domain Mapping plugin to map TLD to subsite using 'www' in mapped URL.

I understand how this is not working, yet was struggling to find a simple way to get it to work as desired.

I believe that I have found a simple solution: modify the RewriteCond to fit a more specific pattern which includes my primary domain
tivism\.com
my thought was changing:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
to something like:
RewriteCond %{HTTP_HOST} ^www\.(.*tivism\.com) [NC]

This seems to be working quite nicely :slight_smile:

Thought I'd run this by y'all for a second opinion :slight_smile:

Live Examples:

http://www.tivism.com --> https://tivism.com

http://www.gfz3whatever.tivism.com --> new registration @ https://tivism.com for 'gfz3whatever', sans-www :slight_smile:

and as desired, the change seems to allow:

http://www.tivism.net ( mapped to https://tivismnet.tivism.com ) is NOT rewitten to non-www address,

same: http://www.tivism.org ( mapped to https://tivismorg.tivism.com )

:slight_smile:

Thoughts?

Cheers,
Max

edit: thought I'd just go ahead and include the whole bit of custom rewrite code that I'm working with here, as it stands now after edits discussed above, as well as my current Domain Mapping settings:

## BEGIN Custom htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# BEGIN Remove www for Network - Allow www for Mapped Domains
RewriteCond %{HTTP_HOST} ^www\.(.*tivism\.com) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# END Remove www for Network - Allow www for Mapped Domains
#BEGIN Subdomains Force SSL
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)\.tivism\.com [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#END Subdomains Force SSL
#BEGIN Network Primary SSL
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^tivism\.com [NC]
RewriteRule ^(.*)$ https://tivism\.com/$1 [R=301,L]
#END Network Primary SSL
</IfModule>
## END Custom htaccess

Primary Domain Mapping Network Settings:
a) Server IP: 185.56.85.36
b) Admin Mapping: Original
c) Login Mapping: Original
d) Cross-Domain: Yes
e) Verify: No
f) Force HTTPS Login/Admin: Yes
g) Force HTTPS Frontend: No

Subsites Domain Mapping Subsite Settings:
h) Protocol: http://
i) Frontend redirect should be: directed to mapped (primary) domain
j) All mapped addresses are currently using 'www'

Only other significant changes made post install:
Primary db values:
SITEURL & HOME https (edited after wp install, before network setup)
Subsite db values:
1) one variance:
1a) tivismnet.tivism.com SITEURL https (edited)
2b) tivismorg.tivism.com SITEURL http (set at subsite creation)

    • wp.network
      • The Bug Hunter

      Hi @aristath

      Thanks for your reply :slight_smile:

      Your Q is excellent, answers are:

      1) it is currently just a project requirement (without getting into the Story)
      2) also, since I ONLY want to allow www with mapped domains and to remove www from requests to primary AND to at least first-level subdomains of primary, this is an interesting htaccess challenge, and
      2a) knowing how to do this is kinda cool
      3) if project need for www in mapped domains changes, can always change htaccess to include mapped domains for www rewite

      RewriteCond %{HTTP_HOST} ^www\.(.*tivism\.com) [NC]
      RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

      the above code seems to work well for me, and I was hoping you might at least be able to look the code over for any thing *obvious* ...?

      I'm feeling pretty confident in it, and am a bit proud of myself for figuring it out... and I also know that I'm still a novice at this, and appreciate your eye over my shoulder if that makes some sense :slight_smile:

      What I'm stumped by is this:
      If I unmap/remap my domains, changing
      Frontend redirect should be: directed to mapped (primary) domain
      (which isn't working quite right at the moment anyways, see thread)
      to
      Frontend redirect should be: disabled and entered domain should be used

      and leave all else as described above... then, after deleting all browser data & flushing local dns cache,

      https://www.original-test.tivism.com
      will be successfully rewritten to
      https://original-test.tivism.com
      with NO security warning?

      and yet

      https://www.tivismnet.tivism.com
      will be successfully rewitten to
      https://tivismnet.tivism.com
      but WITH a security warning!

      https://www.tivismorg.tivism.com
      will be successfully rewitten to
      https://tivismorg.tivism.com
      but WITH a security warning!

      again,
      https://www.original-test.tivism.com
      is rewritten to remove www with NO cert warning while subsites which also have a mapped domain generate a cert warning when requesting original address with www! I don't get this, but obviously suspect something to do with domain mapping... ??

      Cheers,
      Max

      ps. the original-test.tivism.com subsite SITEURL value has NOT been edited, it is set to: http
      ( tivismorg.tivism.com subsite SITEURL value has also NOT been editied, it is set to: http )

      pps. been reading this reference doc, total game changer:
      http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html

  • wp.network
    • The Bug Hunter

    @aristath

    fyi, Support Access is Active if you'd like to poke around at all

    also, I have reverted all changes to SITEURL and HOME values in db (for subsite tivismnet.tivism.com and primary) to default value: http

    so, with current Domain Mapping settings set only for controlling https login/admin, htaccess is only thing controlling frontend https, correct?

    I'd like to be able to remove/rewrite www from requests for subdomain subsites which have a domain mapped, while using https - or at least know why it is not working :slight_smile:

    thanks for your insights/opinions :slight_smile:

    kind regards,
    max

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.