【问题标题】:Simplifying an .htaccess file with several RewriteCond's and RewriteRule's across multiple domains使用跨多个域的多个 RewriteCond 和 RewriteRule 简化 .htaccess 文件
【发布时间】:2021-08-08 11:35:06
【问题描述】:

我在服务器上的同一目录中托管了多个域,每个域都重定向到各自的子目录。我还将所有域重定向到 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/$1 [L,R=301]

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


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

RewriteCond %{HTTP_HOST} ^(ww+\.)?domaintwo\.com
RewriteCond %{REQUEST_URI} !/domaintwo-com/
RewriteRule ^(.*)$ /domaintwo-com/$1 [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/$1 [L,R=301]

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

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

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

如果我尝试在 IfModule 中将 HTTPS 和非 WWW 作为两个单独的重写处理(参见下面的代码),那么重定向会中断,并且我在 Google Chrome 窗口中收到“重定向过多”错误。

  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 redirect mod-rewrite https url-rewriting


    【解决方案1】:

    请确保将这些规则放在您的 .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.
    

    【讨论】:

    • 感谢正则表达式的解释。我以后会再提到这个。保重
    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多