【发布时间】: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