Hi all,
I'm no Apache expert and have some problems creating virtual subdomains.
In short, I want a default controller to be called if the user visits subdomain.example.com. However if the user visits subdomain.example.com/index.php/foo/bar, I want to re-write to .example.com/index.php/foo/bar
subdomain.example.com/index.php/foo/bar ====> .example.com/index.php/foo/bar
subdomain2.example.com/index.php/lol/cats ====> .example.com/index.php/lol/cats
subdomain.example.com/ ====> .example.com/index.php/default
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:
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 on its own):
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]
The individual code snippets work fine. 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:
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.


Reply With Quote


Bookmarks