简体   繁体   English

如何将带有路径的子域重定向到没有此路径的根域

[英]How can I Redirect subdomain with path to root domain without this path

I need help with this case我需要这个案子的帮助

I need to redirect (301) a subdomain test.example.com/?preview_theme=niss-promag to https://example.com without /?preview_theme=niss-promag我需要重定向(301)的子域test.example.com/?preview_theme=niss-promaghttps://example.com而不/?preview_theme=niss-promag

I already used code but it's redirecting to root domain with the same path https://example.com/?preview_theme=niss-promag我已经使用过代码,但它重定向到具有相同路径的根域https://example.com/?preview_theme=niss-promag

This is the code currently used:这是当前使用的代码:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ https://example.com [L,R=301]

Note: I need to redirect whatever path of the subdomain to root domain I don't specify the one above!注意:我需要将子域的任何路径重定向到根域我没有指定上面的那个!

This should be working:这应该有效:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{QUERY_STRING} ^preview_theme=niss-promag$
RewriteRule ^(.*)$ https://example.com [QSD,R=301]

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up.最好从 302 临时重定向开始,然后在确定一切设置正确后才将其更改为 301 永久重定向。 That prevents caching issues while trying things out...这可以防止在尝试时出现缓存问题......

If that does not work then most likely your http server is outdated, try something like that:如果这不起作用,那么很可能您的 http 服务器已过时,请尝试以下操作:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{QUERY_STRING} ^preview_theme=niss-promag$
RewriteRule ^(.*)$ https://example.com/? [R=301]

This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file).此实现将在 http 服务器主机配置或动态配置文件(“.htaccess”文件)中类似地工作。 Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host.显然重写模块需要在http服务器中加载并在http主机中启用。 In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.如果您使用动态配置文件,您需要注意在主机配置中完全启用它的解释,并且它位于主机的DOCUMENT_ROOT文件夹中。

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess").还有一个一般性的评论:你应该总是喜欢将这样的规则放在 http 服务器主机配置中,而不是使用动态配置文件(“.htaccess”)。 Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server.这些动态配置文件增加了复杂性,通常是意外行为的原因,难以调试,而且它们确实会降低 http 服务器的速度。 They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).它们仅在您无法访问真正的 http 服务器主机配置(阅读:非常便宜的服务提供商)或坚持编写自己的规则的应用程序(这是一个明显的安全噩梦)的情况下作为最后一个选项提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM