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


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]
#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

my thought was changing:

RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]

to something like:

RewriteCond %{HTTP_HOST} ^www.(.* [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: –> –> new registration @ for ‘gfz3whatever’, sans-www :slight_smile:

and as desired, the change seems to allow: ( mapped to ) is NOT rewitten to non-www address,

same: ( mapped to )





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.(.* [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} ^(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#END Subdomains Force SSL
#BEGIN Network Primary SSL
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [R=301,L]
#END Network Primary SSL
## END Custom htaccess

Primary Domain Mapping Network Settings:

a) Server IP:

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) SITEURL https (edited)

2b) SITEURL http (set at subsite creation)

    • 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.(.* [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)


    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,

    will be successfully rewritten to

    with NO security warning?

    and yet

    will be successfully rewitten to

    but WITH a security warning!

    will be successfully rewitten to

    but WITH a security warning!


    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… ??



    ps. the subsite SITEURL value has NOT been edited, it is set to: http

    ( subsite SITEURL value has also NOT been editied, it is set to: http )

    pps. been reading this reference doc, total game changer:

    • The Bug Hunter


    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 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,


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.