使用跨多个域的多个 RewriteCond 和 RewriteRule 简化 .htaccess 文件

Simplifying an .htaccess file with several RewriteCond's and RewriteRule's across multiple domains

我在服务器上的同一目录中托管了多个域,每个域都重定向到各自的子目录。我还将所有域重定向到 HTTPS 并通过 .htaccess 文件删除 WWW。一切都按原样工作,但代码感觉很笨拙,我相信它可以简化。任何关于如何这样做、如何删除多余内容的提示,我们将不胜感激。

在我看来,我应该能够普遍处理 HTTPS 和非 WWW 部分,然后只需要对域到子目录部分进行个性化处理。请注意,我还重定向了一个域的变体(.com 和 .net 到 .org 地址),但我认为那里不会发生任何简化。

这里有足够的代码来说明我忙碌的小 .htaccess 文件中发生的一切:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80

# Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} off [OR]
  RewriteCond %{HTTP_HOST} ^www\. [NC]
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>


RewriteCond %{HTTP_HOST} ^www\.domainone\.org [NC]
RewriteRule (.*) https://domainone.org/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(ww+\.)?domainone\.org
RewriteCond %{REQUEST_URI} !/domainone-org/
RewriteRule ^(.*)$ /domainone-org/ [L]


RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule (.*) https://domaintwo.com/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(ww+\.)?domaintwo\.com
RewriteCond %{REQUEST_URI} !/domaintwo-com/
RewriteRule ^(.*)$ /domaintwo-com/ [L]


RewriteCond %{HTTP_HOST} ^(www\.)?domainthree\.net$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domainthree\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainthree\.org [NC]
RewriteRule (.*) https://domainthree.org/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(ww+\.)?domainthree\.org
RewriteCond %{REQUEST_URI} !/domainthree-org/
RewriteRule ^(.*)$ /domainthree-org/ [L]

我无法让 RewriteCond %{HTTP_HOST} ^www\. [NC] 在 IfModule 部分工作。这就是为什么每个域下面都有看起来像冗余代码的原因:

RewriteCond %{HTTP_HOST} ^www\.domainone\.org [NC]
RewriteRule (.*) https://domainone.org/ [L,R=301]

如果我尝试在 IfModule 中将 HTTPS 和非 WWW 处理为两个单独的重写(参见下面的代码),则重定向中断,并且我在 Google Chrome window.

  RewriteCond %{HTTPS} off 
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  RewriteCond %{HTTP_HOST} ^www\. [NC]
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

感谢您的帮助。

请确保将这些规则放在您的 .htaccess 规则文件的顶部。使用您显示的示例,您能否尝试以下操作。请确保在测试您的 URL 之前清除您的浏览器缓存。

Options +FollowSymLinks
RewriteEngine on

# Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

  RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

</IfModule>


如果您只想对特定域执行 https 和非 www,请尝试执行以下操作。请一次仅使用以上或以下规则集。

Options +FollowSymLinks
RewriteEngine on

# Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^(?:www\.)?(domainone\.org|domaintwo\.com|domainthree(?:\.net|\.com))$ [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

  RewriteCond %{HTTP_HOST} ^(?:www\.)?(domainone\.org|domaintwo\.com|domainthree(?:\.net|\.com))$ [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

</IfModule>

说明: 为以上添加详细说明。

^(?:www\.)?         ##Matching from starting of the value, where in a non-capturing group  matching www followed by DOT keeping it optional here.
(                   ##Starting a capturing group here.
 domainone\.org     ##Matching domainone\.org here.
 |                  ##OR condition to match either this or further values.
 domaintwo\.com     ##Matching domaintwo\.com here.
 |                  ##OR condition to match either this or further values.
 domainthree        ##Matching domainthree here.
   (?:              ##Starting a non-capturing group here.
      \.net|\.com   ##Matching dot net OR dot com here.
   )                ##Closing non-capturing group here.
)$                  ##Closing capturing group here.