开发者

Apache: (non)trivial rewrite of subdomains

I have an (at least for me) non trivial task with a project running on an Ap开发者_开发知识库ache2 VHost.

Assuming that I own the domain 'foobar.com' and created a 'wildcard catch all' for all subdmains at my registrars config panel I'd like to achive the following:

Every request going to 'api.foobar.com' should pass. Every request going to '*.foobar.com' where * is everything but api should be rewritten to 'foobar.com'

It seems I am not able to write the correct rewrite condition and rule. Can anyone help?

Thx and regards.

Felix


You can do this like this.

RewiteEngine ON

RewriteCond ${HTTP_HOST} !^api\.foobar\.com$
RewriteCond ${HTTP_HOST} !^foobar\.com$
RewriteRule ^/?(.*)$ "http\:\/\/foobar\.com\/$1" [R=301,L]

Here you are redirecting any url with host other than api.foobar.com OR foobar.com to foobar.com

The above entries append the URL, if it is not desired you can skip the $1 in the RewriteRule directive

Updtae
GeorgieF (OP) suggested the following regex and it shoul also work:

RewriteCond ${HTTP_HOST} !^(api\.)?foobar\.com$ [OR]
RewriteRule ^/?(.*)$ "http\:\/\/foobar\.com\/$1" [R=301,L]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜