apache 基于子域的Apache重写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:43:17  来源:igfitidea点击:

Apache rewrite based on subdomain

apachemod-rewrite

提问by Sam

Im trying to redirect requests for a wildcard domain to a sub-directory.
ie. something.blah.domain.com--> blah.domain.com/something

我试图将通配符域的请求重定向到子目录。
IE。something.blah.domain.com-->blah.domain.com/something

I dont know how to get the subdomain name to use in the rewrite rule.

我不知道如何获取子域名以在重写规则中使用。

Final Solution:

最终解决方案:

RewriteCond %{HTTP_HOST} !^blah\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*) /%1/ [L]

Or as pointed out by pilif

或者正如 pilif 指出的那样

RewriteCond %{HTTP_HOST} ^([^.]+)\.media\.xnet\.tk$

回答by pilif

You should have a look at the URL Rewriting Guidefrom the apache documentation.

您应该查看apache 文档中的URL Rewriting Guide

The following is untested, but it should to the trick:

以下是未经测试的,但它应该是诀窍:

RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.domain\.com$
RewriteRule ^/(.*)$           http://blah.domain.com/%1/ [L,R] 

This only works if the subdomain contains no dots. Otherwise, you'd have to alter the Regexp in RewriteCond to match any character which should still work due to the anchoring, but this certainly feels safer.

这仅在子域不包含点时才有效。否则,您必须更改 RewriteCond 中的 Regexp 以匹配由于锚定而仍应起作用的任何字符,但这当然感觉更安全。

回答by BlaM

Try this:

尝试这个:

RewriteCond %{HTTP_HOST} (.+)\.blah\.domain\.com
RewriteRule ^(.+)$ /%1/ [L]

@pilif (see comment): Okay, that's true. I just copied a .htaccess that I use on one of my projects. Guess it has a slightly different approach :)

@pilif(见评论):好的,这是真的。我刚刚复制了我在我的一个项目中使用的 .htaccess。猜猜它的方法略有不同:)

回答by pilif

@Sam

@山姆

your RewriteCond line is wrong. The expansion of the variable is triggered with %, not $.

你的 RewriteCond 行是错误的。变量的扩展由 % 触发,而不是 $。

RewriteCond %{HTTP_HOST} ^([^\.]+)\.media\.xnet\.tk$
            ^

that should do the trick

这应该够了吧