简体   繁体   English

如何使用 htaccess 将 url 重定向到动态子域?

[英]How to redirect urls to dynamic subdomain using htaccess?

First of all.I would like to ask that how to make subdomain redirection using htaccess.首先,我想问一下如何使用htaccess进行子域重定向。 www.samcheck.com/www/google.com to google.com.samcheck.com www.samcheck.com/www/google.com 到 google.com.samcheck.com

I have tried all the solution available on the stackoverflow.But none of the solutions worked for me.我已经尝试了 stackoverflow 上所有可用的解决方案。但没有一个解决方案对我有用。

As well as the URL is https://sampcheck.com/www/google.com , I want it to be https://google.com.sampcheck.com so that it shows the content of main url.除了 URL 是https://sampcheck.com/www/google.com ,我希望它是https://google.com.sampcheck.com以便它显示主 URL 的内容。

RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule . index.php [L]
        RewriteCond %{HTTP_HOST} ^.sampcheck.com
RewriteCond %{HTTP_HOST} www\.([^.]+)\.sampcheck\.com [NC]
RewriteRule ^(.*)$ http://sampcheck.com/www/$1 [L,NC,QSA]

In addition to my comments to the question and my puzzlement what sense such host naming scheme should make (except for cheap phishing attempts), here is something that should point you into the right direction:除了我对这个问题的评论和我的困惑之外,这种主机命名方案应该有什么意义(廉价的网络钓鱼尝试除外),这里有一些东西应该指向正确的方向:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.samcheck\.com$
RewriteRule ^/?www/([^/]+)/?$ https://www.$1.sampcheck.com/ [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...这可以防止在尝试时出现缓存问题......

Also note that such redirection can only work if you actually have valid certificates for those dynamic host names...另请注意,只有当您确实拥有这些动态主机名的有效证书时,此类重定向才有效...

This rule 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