Redirect wildcard subdomain without known domain - htaccess

六月ゝ 毕业季﹏ 提交于 2020-01-15 09:41:29

问题


What is the appropriate way to redirect via htaccess when a top level domain name is not known?

My specific situation is this... Our company has a custom content management system that is used on several of our servers. Each server had a subdomain for each of our customers on that server.

i.e.

  • customerBob.exampleA.com
  • customerLarry.exampleA.com
  • customerJudy.exampleB.com
  • customerAlice.exampleB.com

We have many servers, each one hosting a group of customers. The subdomains were completely unnecessary, as customers can already access their account from the top level domain. It just makes support more complicated and help files messier.

We would like to eliminate all subdomains and redirect the subdomain to the main domain.

So if one visits: customerBob.exampleA.com/test.php --> they should be directed to --> exampleA.com/test.php.

I would like to wildcard the top level domain. All the examples I have seen for htaccess redirects show example.com specified in the destination. Is it possible for me to redirect to the top level domain without knowing what that top level domain might be?

This is the code someone came up with, and it completely fails, I am getting a 500 at this point:

RewriteEngine On RewriteCond %{HTTP_HOST} ^([^/.]+)\.(*)\.(*)$ RewriteRule (.+)$ "/%1" [L,P]

I assume this is possible since there are many content management systems in existence where this might be necessaryThank you for your help.


回答1:


I've made a small change to the code you were provided:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^/.]+)\.(.*)\.(.*)$ [NC] 
RewriteRule ^(.*)$ https://%2\.%3/$1 [L,R]

For me this is returning http://exampleA.com/test.php. I'm explicitly rewriting to http. You might want to change this if you use SSL.

Make sure you clear your cache before testing this.



来源:https://stackoverflow.com/questions/51728083/redirect-wildcard-subdomain-without-known-domain-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!