I'm building an appointments booking application in CodeIgniter, where small businesses sign up and get their own subdomain, which their customers can access and book appointments. Now, I want to pass any wildcard subdomain as a parameter to the appointment controller's index method, such that:
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"
I have set up wildcard subdomains in cPanel and pointed the document root to public_html
, where CodeIgniter's index.php
resides.
My .htaccess
looks like this (taken from this answer):
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]
This works for the subdomain part, and the controller's action correctly receives the subdomain as the first parameter. However, in a URL with additional method parameters at the end, such as http://wildcard.example.com/1234
, the 1234
parameter is not being recognized by the controller's action. How do I write the rules to pass the subdomain as the controller's first parameter, and any other "leftover" segments as additional, optional parameters to the controller's action?