简体   繁体   中英

How Can I Redirect ALL URLs to www and only specific URLs to https for Multisite Wordpress

I have multiple domains running on a Wordpress installation, with other sites also hosted on the same space that are pure html.

I want to create a set of htaccess rewrite rules that prepend "www" to a URL if it doesn't have it already, and then redirect to https for specific domain names, but not others.

I had written the following code, which seems logically correct to me, but causes issues when I deploy it:

# Check to see if the hostname begins with www. If not, add "www" then move to the next Rule
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 
# Check to see if the hostname is one of three different domains, using RegEx
# If they are, then change to "https",permanently redirect and stop processing rules
    RewriteCond %{HTTP_HOST} ^www\.(domain1\.co|domain2\.co|domain3)\.uk [NC]
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
# Check to see if the protocol is https AND if the hostname is not one of the https domains
# If they are, then change to "http", permanently redirect and stop processing rules
    RewriteCond %{HTTPS} on [NC]
    RewriteCond %{HTTP_HOST} !^www\.(domain1\.co|domain2\.co|domain3)\.uk [NC]
    RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

#BEGIN WordPress
    RewriteBase /
    RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
   RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

   RewriteCond %{REQUEST_FILENAME} -f [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^ - [L]
   RewriteRule ^(wp-(content|admin|includes).*) web/$1 [L]
   RewriteRule ^(.*\.php)$ web/$1 [L]
   RewriteRule . index.php [L]
# END WordPress

The errors I am getting seem to suggest I am getting a redirect loop which results in none of the Wordpress sites being available. Any clues?

The following is my solution.

The first Rewrite says:

GIVEN THAT the URL is http and not https THEN rewrite the URL so that it starts with "https://" followed by the URL and any folder/file request.

The second Rewrite says:

GIVEN THAT the URL starts with one or more characters, followed by a dot, followed by either "co.uk", "org.uk", "uk", "com" or "org" THEN rewrite the URL so that it starts with "https://www." followed by the domain name and any folder/file request.

# BEGIN RewriteRules
<IfModule mod_rewrite.c>
    RewriteEngine On
# Check if URL is http and redirect to https
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTP:X-Forwarded-SSL} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Check if Host is the root domain and prepend www if it hasn't (e.g. example.com > www.example.com; test.example.com > test.example.com)
    RewriteCond %{HTTP_HOST} ^[^.]+\.(co\.uk|org\.uk|uk|com|org)+$
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

</IfModule>
# END Rewrite Rules

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