开发者

Rewrite <sub>.domain.com/<path> to /subdomains/<sub>/<path>

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)

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


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 underst开发者_如何学编程and 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]

I hope that worked correctly for you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜