【问题标题】:How do i redirect only root domain and ignore all wildcard subdomains?如何仅重定向根域并忽略所有通配符子域?
【发布时间】:2023-03-21 16:34:01
【问题描述】:

我查看了一堆关于此的 stackoverflow 页面和非 stackoverflow 网站,并尝试了很多东西。没有任何效果。

我需要 my-example.net 才能访问 example.com,我需要 www.my-example.net 才能访问 example.com,我需要忽略所有其他不属于 www.my-example.net 的子域并且不进行重定向.我将来可能想排除某些其他子域,这就是为什么其中一次尝试有 4 个子域例外,但现在让我们专注于忽略除 www 之外的所有子域。 www 和非 www 应该转到不同的域。如果我们可以保持路径的其余部分完好无损,如果不能,则转到第二个域的根目录就可以了。例如:

http://my-example.net/some/path/file.php?parameter=value can go to http://example.com/some/path/file.php?parameter=value, 
but if it's to difficult with wildcard subdomains, then going to http://example.com/ is fine.

您知道,我还有其他规则可以以特定方式处理通配符子域,并且效果很好。一些尝试似乎有效,但打破了我在子域上的其他规则集。例如,假设我有 anysubdomain.my-example.net 去 my-example.net/folder/file.php?anysubdomain&domain=my-example.net ...那么下面的一个或一些尝试会做的是让子域这样做,这不是我想要的:example.com/folder/file.php?anysubdomain&domain=example.com

子域应使用长路径和主域重定向到自己的域,www 子域应重定向到辅助域(保存或不保存路径的其余部分)

这是我的尝试(每个行空间都意味着新的尝试,但在几次尝试中,并不是每一行都应该取消注释 - 我正在尝试各种尝试):

#RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
#RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]

#RewriteCond %{HTTP_HOST} ^my-example\.net$
#RewriteRule ^(.*)$ http://example.com/$1 [L,QSA,R=301]

#RewriteCond %{HTTP_HOST} ^my-example\.net$ [NC]
#RewriteRule ^. http://example.com/ [L,R=302]

#RewriteCond %{HTTP_HOST} ^(my-example\.net)$ [NC]
#RewriteRule ^. http://example.com/ [L,R=302]

#RewriteCond %{HTTP_HOST} my-example\.net [NC]
#RewriteCond %{REQUEST_URI} ^/$
#Rewriterule ^(.*)$ http://example.com/ [L,R=301]

#RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
#RewriteCond %{HTTP_HOST}   !^$
#RewriteRule ^/?(.*)         http://example.com/$1 [L,R,NE]

#RewriteCond %{HTTP_HOST} ^my-example\.net$ [NC]
#RewriteCond %{HTTP_HOST} !^(.+)\.my-example\.net$ [NC]
#RewriteRule ^(.*)$ http://example.com/$1 [L,QSA,R=301]

#RewriteCond %{HTTP_HOST} !^www
#RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteCond %{REQUEST_URI} ^(application|system)
#RewriteRule .? index.php?/%{REQUEST_URI} [L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule .? index.php?/%{REQUEST_URI} [L]

#RewriteCond %{HTTP_HOST} ^www\.my-example\.net$
#RewriteCond %{HTTP_HOST} ^www\.my-example\.net$ [OR]
#RewriteCond %{HTTP_HOST} ^my-example\.net$
#RewriteRule ^ http://example.com%{REQUEST_URI} [R=301,L,NE]
# Extract the subdomain part of domain.com
#RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.my-example\.net$ [NC]
# Check that the subdomain part is not www, dev, old, or new
#RewriteCond %1 !^(www|dev|old|new)$ [NC]
# Redirect all requests to the original url /blog
#RewriteRule ^.*$ /%1 [L]

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    在提及 ServerName my-example.net 的 VirtualHost 子句中,配置重定向到 example.com。

    在提及ServerName www.my-example.net 的 VirtualHost 子句中,进行类似配置。

    “所有其他子域”部分涉及 DNS A 记录(例如“*”通配符)和 Apache 配置。首先,使用digping 来查看子域“foo”是否产生了您的网络服务器的A(或AAAA)记录,也就是说,看看Apache 是否甚至可以做出决定。然后,为 foo 添加一个 VirtualHost 子句,或者仅依赖 Apache 的通配符默认匹配,以根据需要提供 200 或 302 或 404 文档。

    DNS 控制客户端的 GET 请求是否会到达端口 80 / 443。客户端在请求中发送“Host: foo.my-example.net”标头。 Apache 的 VirtualHost 参数被传递给strcmp(),将其与客户端的“Host:”标头进行比较。该比较的结果控制 VirtualHost 子句的其余部分是否将用于此请求。

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 2020-09-27
      • 1970-01-01
      • 2018-02-05
      • 2021-09-09
      相关资源
      最近更新 更多