简体   繁体   中英

.htaccess redirect: wordpress multsite root domain http://example.com redirect to http://www.example.com ONLY ON ROOT

Basically, i still need the root domain to work in all sub-directories and can't use an apache VirtualHosts redirect because it will invalidate the multisite admin domain, but I don't want users to see the domain without www.

How can this be accomplished?

Redirect plugins won't allow me to redirect from the root domain because they run on the site (w/ www.) rather than the admin root.

I don't want it to redirect all "www."-less urls, only the root.

If you are using Apache 2.4 or newer you could use IF cases within your .htaccess file, test the following:

<If "%{HTTP_HOST} == 'yourdomain.com'">
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteCond %{SERVER_ADDR} !=127.0.0.1
        RewriteCond %{SERVER_ADDR} !=::1
        RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    </IfModule>
</If>

This should give an 500 server error if you do not have Apache 2.4 installed though, just delete it and your site should work again. I do not know how to solve it for older Apache versions though. I am using this solution for an multisite sub-domain install for 301 redirects, its working pretty good!

I wound up using the following:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://mysecondsite.com/ [L,R=301]

as it preserves the root domain's subdirectories, allowing you to manage the multisite admin dashboard.

It's almost identical to Panama Jack's answer - honestly I'm not sure what the REQUEST_URI does...

Source: How to redirect root and only root via htaccess?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM