【问题标题】:.htaccess Redirections.htaccess 重定向
【发布时间】:2010-09-21 08:48:36
【问题描述】:

我一直在谷歌搜索 .htaccess 重定向信息,但没有找到我想要的。

基本上,我想要一个解决方案,它将采用站点 example.com 并允许您输入 URL 之类的:

 123.example.com
 ksdfkjds.example.com
 dsf38jif348.example.com

这会将他们重定向到:

 example.com/123
 example.com/ksdfkjds
 example.com/dsf38jif348

所以基本上接受任何子域并自动重定向到具有该子域名称的域根目录上的文件夹。

【问题讨论】:

    标签: .htaccess redirect directory subdomain


    【解决方案1】:

    试试这样的:

    # If we're not on http://example.com
    RewriteCond %{HTTP_HOST} .+\.example.com
    
    # Add the host to the front of the URL and chain with the next rule
    RewriteRule ^(.*)$ ${HOST}$1 [C,QSA]
    
    # Make the host a directory
    RewriteRule ^(.*)\.example\.com(.*)$ http://example.com/$1$2 [QSA]
    

    你没有说 http://foo.example.com/bar?moo 会发生什么 - 我已经把它转到 http://example.com/foo/bar?moo 如果这不是您想要的,请更改最后一行。

    【讨论】:

      【解决方案2】:

      如果你只是想让它们成为入口:

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

      否则:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        相关资源
        最近更新 更多