CodeIgniter Forums
HTAccess Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: HTAccess Question (/thread-47043.html)



HTAccess Question - El Forum - 11-24-2011

[eluser]benjamin[/eluser]
Hi all,

I created virtual subdomains using .htaccess. If the user visits subdomain.example.com, then he will be re-directed to www.example.com/index.php/foo/bar:

Code:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/index.php/foo/bar/%1 [P,L]

All good so far.

However if users already specified a controller, then I want the url to be re-written to include this controller. E.g.
subdomain.example.com/index.php/lolcats ==> example.com/index.php/lolcats
I edit the .htaccess to include this code (which works in itself):
Code:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com/index\.php/([.*]) [NC]
RewriteRule ^index.php/(.*)$ http://www.example.com/index.php/%1 [P,L]

However I'm running into problems when I combine the two rewrite rules. Then, only the former works, despite me using L to ignore any subsequent rules.

My .htaccess:

Code:
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com/index\.php/([.*]) [NC]
RewriteRule ^index.php/(.*)$ http://www.example.com/index.php/%1 [P,L]

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/index.php/foo/bar/%1 [P,L]

Any help would be greatly appreciated.