如何实现动态子域codeigniter
with .htaccess
?
确保您的网站上启用了子域。当您输入 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 的方法不只一种。
所以如果你使用上面的规则它会给你漂亮的 URI更重要的是,会给你唯一的 URI。因此,如果您使用上述规则,您将只能获得一个注册页面的 URI,即http://yoursite.com/signup
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)