简体   繁体   English

对于多站点 Wordpress,我如何将所有 URL 重定向到 www,并且仅将特定 URL 重定向到 https

[英]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.

相关问题 如何将我所有与协议相关的URL重定向到https:// url? - How do I redirect ALL my protocol-relative URLs to https:// urls? 如何正确重定向除某些特定URL外的所有.html URL - How to correctly redirect all .html URLs except some specific URLs Wordpress多站点强制使用域HTTPS和WWW,仅使用HTTPS子站点HTACESS - Wordpress multisite Force HTTPS and WWW for domain and only HTTPS for subdomains HTACESS 如何使用.htaccess规则将所有传入的HTTP URL重定向到WordPress中的https - How to Redirect all incoming http urls to https in WordPress using .htaccess rule 如何在静态网址中捕获ID并将其重定向到Wordpress中的新网址[301] - How can I catch an id in static urls and redirect it to new urls in wordpress [301] 我如何才能将 wordpress 网站网址克隆到新网址? - How i can clone wordpress site urls only to new one? WordPress URL 不以斜杠结尾重定向到 HTTPs 主页 - WordPress URLs not ending with a slash redirect to HTTPs homepage 如何将“ http” URL重定向到新的“ https” URL(重定向301)? - How can I redirect my 'http' URLs to my new 'https' URL (Redirect 301)? 应该为WordPress网站添加哪些Cloudflare页面规则以仅在HTTPS裸(非www)URL上运行? - What Cloudflare page rules should be added for a WordPress website to run on HTTPS naked (non www) URLs only? WordPress多站点WWW重定向问题(仅根站点) - Wordpress Multisite WWW redirect issue (Root site only)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM