将通配符子域作为参数传递给CodeIgniter

时间:2015-05-01 20:36:12

标签: .htaccess codeigniter mod-rewrite

我正在CodeIgniter中建立一个约会预订应用程序,小企业在那里注册并获得自己的子域名,客户可以访问这些子域名并预约约会。现在,我想将任何通配符子域作为参数传递给约会控制器的索引方法,例如:

http://wildcard.example.com --> http://example.com/appointments/book/wildcard --> Appointments::book($sd = "wildcard", $id = ''); //books an appointment with a company named "wildcard"
http://wildcard.example.com/1234 --> http://example.com/appointments/book/wildcard/1234 --> Appointments::book($sd = "wildcard", $id = "1234"); //Fetches an appointment record 1234 for the company "wildcard"

我在cPanel中设置了通配符子域,并将文档根指向public_html,其中CodeIgniter的index.php位于其中。

我的.htaccess看起来像这样(取自this回答):

Options +FollowSymlinks
RewriteEngine On

#Wildcard subdomains
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ^(.+).example.com
RewriteRule ^([^/]*)$ http://example.com/appointments/index/%1 [P,L,QSA]
#end Wildcard subdomains

#default CodeIgniter rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

这适用于子域部分,控制器的操作正确地接收子域作为第一个参数。但是,在最后包含其他方法参数的URL(例如http://wildcard.example.com/1234)中,控制器的操作无法识别1234参数。如何编写规则以将子域作为控制器的第一个参数传递,并将任何其他“剩余”段作为控制器操作的附加可选参数?

0 个答案:

没有答案