【问题标题】:Redirecting subdomains to their corresponding folders将子域重定向到其对应的文件夹
【发布时间】:2016-02-08 22:37:56
【问题描述】:

是否可以配置.htaccess,这样任何子域都将被重定向到它的相应文件夹,而无需每次我想添加子域时编辑.htaccess 文件?怎么样?

foo.domain.com -> /subdomains/foo
bar.domain.com -> /subdomains/bar

anything.domain.com/file.txt -> /subdomains/anything/file.txt

谢谢!

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    您可以在站点根目录 .htaccess 中使用此规则:

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
    RewriteRule ^((?!subdomains/).*)$ subdomains/%1/$1 [L,NC]
    

    【讨论】:

    • 你搞定了吗,马丁?我正在尝试做一些非常相似的事情(获取 example.mydomain.com 以使用存储在 www.mydomain.com 下的 php)并且我发现重写:RewriteCond %{HTTP_HOST} ^example.mydomain.com$RewriteRule ^something.html$ /public_html/action/something.php [L](www 站点存储在public_html 和示例站点存储在其姊妹文件夹中,以上位于 2 个文件夹的父级中的 .htaccess 中)不起作用
    • (字符用完)但我可以使用重写发送到另一个域:RewriteRule ^something.html$ http://www.example.com/something.php [L] 有效。我不认为该站点可以从更高的文件夹级别访问 PHP?有没有人得到这个工作? (为什么格式化不起作用?!?:()
    • @LeadeGroot:建议你打开一个新问题,因为从 cmets 读取 .htaccess 并不容易。
    猜你喜欢
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2017-05-10
    相关资源
    最近更新 更多