重写<sub>.domain.com/<path>到/subdomains/<sub>/<path>; [英] Rewrite <sub>.domain.com/<path> to /subdomains/<sub>/<path>
问题描述
我被我的 mod_rewrite 卡住了(大约 4 个小时,是的,我也用谷歌搜索了).基本上我想创建动态子域.所以我只是创建一个新文件夹,它会自动拥有自己的子域.我给你举个例子:
I've got stuck with my mod_rewrite (for about 4 hours, yes, I googled, too). Basically I want to create dynamic subdomains. So I just create a new folder and it automatically has it's own subdomain. Let me give you an example:
/ (root)
/logs
/configs
/otherPrivateStuff
[...]
/subdomains (This is, where things get interesting)
/www (should be: www.domain.com) [domain.com is automatically 301 to www]
/aproject (should be: aproject.domain.com)
/anotherproject (should be: anotherproject.domain.com)
我的 .htaccess 如下所示:
My .htaccess looks like this:
# Mod Rewrite
RewriteEngine on
# Subdomains
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com(:[0-9]+)? [NC]
RewriteRule ^(.*) subdomains/%1/$1/ [NS,L]
http://project.domain.com/path/subpath/file.txt --> http://domain.com/subdomains/project/path/subpath/file.txt
这就像一个魅力,但有一个问题:这条规则不适用,如果网址看起来像这样:http://project.domain.com/subdomains/ 这就像根本没有重写一样.
This works like a charm, but there is a problem: this rule does not apply, if the url looks like this: http://project.domain.com/subdomains/ This beahaves like there's no rewriting at all.
我不明白这个.这里有人可以帮助我吗?:)
I do not understand this. Can anybody out here help me? :)
推荐答案
我很确定您想专门排除子域"目录.为此,您只需使用:
I'm pretty sure you want to specifically exclude the "subdomains" directory. To do this, you can simply use:
# Mod Rewrite
RewriteEngine on
# Subdomains
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com(:[0-9]+)? [NC]
RewriteCond %{REQUEST_URI} !^/subdomains
RewriteRule ^(.*) subdomains/%1/$1/ [NS,L]
希望这对您有用.
这篇关于重写<sub>.domain.com/<path>到/subdomains/<sub>/<path>;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!