【问题标题】:Dynamic htaccess handling of subdomain, missing traffic & HTTPS子域、丢失流量和 HTTPS 的动态 htaccess 处理
【发布时间】:2020-01-27 05:38:39
【问题描述】:

我对@9​​87654321@ 和 RegEx 还是很陌生,对此感到非常沮丧,但我可能过于复杂了。基本上:

  • HTTP_HOST 将是多个域之一,应按原样保留,包括子域,except www. 应始终删除
  • 只有domain1 和'domain2' 有SSL,所以HTTPS 应该被强制使用,但其他任何人都应该被强制使用HTTP
  • 如果域名后面的第一个子文件夹foo,则重写,使foo是子域而不是子文件夹。
  • 之后,如果foo. 是子域
    • 保留可见 URL 中所有丢失/禁止的文件夹/文件(稍后处理)
    • 其中任何一个的实际页面位于foo.*.com/index.php
  • foo 子域上的

    缺失/禁止页面 not 仍应发送到根目录中的 \index.php,我目前正在这样做:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ /index.php [last,nocase]
    

我的尝试:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www. [NC] 
RewriteRule ^(.*)$ $1 [L] 
RewriteCond %{HTTP_HOST} domain1\.ca [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} !domain1\.ca [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteCond %{REQUEST_URI} ^/foo.* [NC]
RewriteRule ^ %{REQUEST_SCHEME}://foo\.%{HTTP_HOST}%{REQUEST_URI} [L]
RewriteRule "^/foo/(.+)" "%{REQUEST_SCHEME}://foo.%{HTTP_HOST}/$1" [L,NS,QSA,R=301]

一些例子:

incoming url:                               should become:
http://www.domain1.com/foo/blah          => https://foo.domain1.com/blah
https://example.com/foo/blah.html        => http://foo.example.com/blah.html
http://www.domain1.com/foo/index.php/foo => https://foo.domain1.com/foo
https://example.com/blah/blah.html       => http://example.com/blah/blah.html 

我希望这是有道理的(我不知所措,迟到了!)- 谢谢!

【问题讨论】:

  • “但任何其他人都应该强制使用 HTTP” - “来自”哪里?如果其中任何一个都没有有效的证书,那么您首先不会收到任何传入的 HTTPS 请求。
  • 按原样保留主机名与 /foo 文件夹 - https://bar.example.com/foo/blah.html 会发生什么?还是这两种情况相互排斥?
  • @04FS - 好主意;我不希望任何流量获得 that 的 url,但如果我必须选择,我宁愿放弃 bar. 并将 url 显示为 https://foo.example.com/blah.html(但仍显示实际内容来自index.html)。这有意义吗?

标签: .htaccess url redirect mod-rewrite traffic


【解决方案1】:

这些规则是基于您的问题的顺序翻译

RewriteEngine On

# Remove www from domain, keep scheme (http or https)
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [R=302,L]

# force https for {domain1,domain2}
RewriteCond %{HTTP_HOST} ^(?:domain1|domain2)\. [NC]
RewriteCond %{HTTPS} off [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# force http for others
RewriteCond %{HTTP_HOST} !^(?:domain1|domain2)\. [NC]
RewriteCond %{HTTPS} on [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# foo subfolder -> foo subdomain
RewriteCond %{HTTP_HOST} !^foo\. [NC]
RewriteCond %{REQUEST_URI}#%{HTTPS}s ^/foo/([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^ http%2://foo.%{HTTP_HOST}/%1 [R=302,L]

# hide index.php for foo subdomain
RewriteCond %{HTTP_HOST} ^foo\. [NC]
RewriteRule ^index\.php(?:$|/(.*)) /$1 [R=302,L]

# (internal rewrite) foo subdomain -> foo's root index
RewriteCond %{HTTP_HOST} ^foo\. [NC]
RewriteCond %{REQUEST_URI} !^/foo/ [NC]
RewriteRule ^ /foo/index.php [L]

# default rule (root index)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]

这很丑,我同意。但这应该可以按您的预期工作。它已经在本地服务器上进行了测试,并且您显示的所有示例测试都通过了。

注意:我故意使用302 重定向。当一切都在你身边时,只需切换回301

【讨论】:

  • 太棒了,很抱歉延迟响应,但到目前为止看起来很棒。谢谢@贾斯汀!
  • 嘿@Justin,实际上并不是我需要它强制域使用HTTPS。在# force https for {domain1,domain2} 部分(以及以下部分)中,onoff 可以颠倒吗?我也应该只用 SLD (domain1) 而不是 TLD (.com) 替换 domain1
  • could the on and off be reversed 是什么意思?无论如何,是的,这只是 SLD 部分。
猜你喜欢
  • 2010-11-06
  • 1970-01-01
  • 2018-11-29
  • 2014-05-22
  • 1970-01-01
  • 2012-05-04
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多