1

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?

Community
  • 1
  • 1
Agent.Logic_
  • 1,553
  • 2
  • 25
  • 36
  • You don't save source url at all. Put it : RewriteRule ^([^/]*)$ http://example.com/appointments/index/%1/$1 [P,L,QSA] – splash58 May 01 '15 at 20:48
  • @splash58 This is the answer, it works! Please add this as an answer so I can upvote and accept it. – Agent.Logic_ May 01 '15 at 20:55

1 Answers1

2

You don't save source uri at all. Let's add it to the end of redirected uri :

RewriteRule ^([^/]*)$ example.com/appointments/index/%1/$1 [P,L,QSA]
splash58
  • 26,043
  • 3
  • 22
  • 34