重写 .domain.com/到 /subdomains//

发布于 2024-11-10 08:45:35 字数 1158 浏览 0 评论 0原文

我被我的 mod_rewrite 困住了(大约 4 个小时,是的,我也在 google 上搜索过)。 基本上我想创建动态子域。所以我只需创建一个新文件夹,它就会自动拥有自己的子域。让我给你举个例子:

/ (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 看起来像这样:

# 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/ 这就像根本没有重写一样。

我不明白这一点。这里有人可以帮助我吗? :)

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 understand this. Can anybody out here help me? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2024-11-17 08:45:35

我很确定您想专门排除“子域”目录。为此,您只需使用:

# Mod Rewrite
RewriteEngine on

# Subdomains
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com(:[0-9]+)? [NC]
RewriteCond %{REQUEST_URI} !^/subdomains
RewriteRule ^(.*) subdomains/%1/$1/ [NS,L]

我希望这对您来说正确。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文