动态处理子域名、缺失流量和HTTPS的htaccess配置

8

我对.htaccess和正则表达式还不太熟悉,但我很沮丧,可能是因为我过于复杂化了。基本上:

  • HTTP_HOST will be one of several domains, and should be preserved as-is including subdomains, except www. should always be removed
  • only domain1 and 'domain2' have SSL, so HTTPS should be forced, but any others should be forced to HTTP
  • if the first subfolder after the domain name is foo, then rewrite so that foo is a subdomain instead of a subfolder.
  • after that, if foo. is the subdomain:
    • retain any missing/forbidden folders/file in the visible URL (to be handled later)
    • the actual page for any of these is located at foo.*.com/index.php
  • missing/forbidden pages not on the foo subdomain should still be sent to \index.php in the root, which I'm currently doing with:

    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请求。 - 04FS
1
保留主机名还是/foo文件夹 - https://bar.example.com/foo/blah.html应该发生什么?或者这两种情况是互相排斥的吗? - 04FS
@04FS - 不错的想法;我不指望任何流量会得到那个 URL,但如果必须选择,我更愿意去掉 bar. 并显示 URL 为 https://foo.example.com/blah.html(但仍然显示从 index.html 中实际内容)。这有意义吗? - ashleedawg
1个回答

4
那些规则是根据您的问题进行的顺序翻译。
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! - ashleedawg
嘿@Justin,实际上我不需要强制将域名转换为HTTPS。在“#force https for {domain1,domain2}”部分(以及其后面的部分),可以将“on”和“off”反转吗?另外,我应该用仅SLD(“domain1”)而不是TLD(“.com”)替换“domain1”吗? - ashleedawg
could the on and off be reversed” 是什么意思?不管怎样,是的,这只是SLD部分。 - Justin Iurman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接