基于子域的Apache重写 [英] Apache rewrite based on subdomain

查看:25
本文介绍了基于子域的Apache重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将对通配符域的请求重定向到子目录.
IE.something.blah.example.com -->blah.example.com/something

I'm trying to redirect requests for a wildcard domain to a sub-directory.
ie. something.blah.example.com --> blah.example.com/something

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

I don't know how to get the subdomain name to use in the rewrite rule.

最终解决方案:

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

或者如pilif所指出的

Or as pointed out by pilif

RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.example\.com$

推荐答案

你应该看看 URL Rewriting Guide 来自 apache 文档.

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

以下内容未经测试,但应该能解决问题:

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

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

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

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.

这篇关于基于子域的Apache重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆