【问题标题】:How to implement dynamic subdomains in codeigniter with .htaccess?如何使用 .htaccess 在 codeigniter 中实现动态子域?
【发布时间】:2012-04-01 16:57:54
【问题描述】:

如何使用.htaccesscodeigniter 中实现动态子域?

【问题讨论】:

  • -@santosh 一直对这个很好奇,谢谢分享,干得好!
  • @santosh:谢谢,非常有用。
  • 如果您以问答形式提出这个问题,那将是理想的选择,因为它现在出现在未回答的列表中。提出和回答您自己的问题(并将其标记为已接受)是完全可以接受的。您不会欺骗系统,如果您的问题和回答可能对其他人有所帮助,系统会受到积极鼓励。 (尽管如此 - 谢谢你)

标签: .htaccess mod-rewrite codeigniter-2 subdomain wildcard-subdomain


【解决方案1】:

确保在您的网站上启用了子域。当您输入 test.yoursite.com 时,它应该会将您带到您网站的欢迎页面。如果相反,它给出了 DNS 查找错误,则表示您的站点上未启用子域。

要在您的站点上启用子域,请将 *.yoursite.com 条目添加到 DNS 区域记录。

第二次在您的.htaccess 文件中插入以下代码并适当地替换yoursite

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #codeigniter specific rule
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #codeigniter specific rule
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #this rule removes www from the URL if its used
    RewriteCond %{HTTP_HOST} ^www.
    RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L]

    #the auth system URIs which don't have subdomains
    RewriteCond %{HTTP_HOST} ^yoursite.
    RewriteRule ^(signup|signin|forgot_password)/?$ index.php?/auth/$1 [L]

    #this rule handles the subdomains
    RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com
    RewriteRule ^(.*)$ index.php?/public_site/%1/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

如有必要,添加更多规则以处理身份验证系统或子域。我知道我这样做是为了让我的网站获得漂亮的 URI。

重要提示:

我发现默认情况下,子域 URI 在没有任何上述规则的情况下与 codeigniter 一起工作得很好。您可以使用test.yoursite.com/# URI 而不是www.yoursite.com/# 访问您的所有站点。但是 URI 并不美观,而且访问 URI 的方法不止一种。

所以如果你使用上面的规则,它会给你漂亮的 URIs,更重要的是,它会给你独特的 URIs。因此,如果您使用上述规则,您将只会获得一个注册页面的 URI,即http://yoursite.com/signup

【讨论】:

    【解决方案2】:

    此代码适用于我的网站,您可以将您的网站移动到您的其他域或文件夹(任何您想要的)。

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond $1 !^(index\.php|application/assets|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    </IfModule>
    

    【讨论】:

      【解决方案3】:

      此规则处理子域

      RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com.  should probably also include the hypen -.  RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).yoursite.com.
      

      【讨论】:

        猜你喜欢
        • 2015-10-09
        • 1970-01-01
        • 2014-03-08
        • 2014-05-22
        • 1970-01-01
        • 1970-01-01
        • 2013-11-19
        • 2012-12-17
        • 2016-07-07
        相关资源
        最近更新 更多