[英]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.我在 Wordpress 安装上运行了多个域,其他站点也托管在纯 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.我想创建一组 htaccess 重写规则,如果 URL 还没有,则将“www”添加到 URL 之前,然后重定向到特定域名的 https,而不是其他域名。
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.我得到的错误似乎表明我得到了一个重定向循环,导致没有任何 Wordpress 站点可用。 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.
鉴于 URL 是 http 而不是 https,然后重写 URL,使其以“https://”开头,后跟 URL 和任何文件夹/文件请求。
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."
鉴于 URL 以一个或多个字符开头,后跟一个点,后跟“co.uk”、“org.uk”、“uk”、“com”或“org”,然后重写 URL 使其开始带有“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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.