tags:

views:

942

answers:

3

I'm trying to implement a solution using .htaccess and wildcard subdomains so that

http://subdomain.example.com is mapped to http://example.com/index.php/accounts/subdomain/. My rules look something like:

RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*/) /index.php [PT,L]

Which works, but disregards everything else. When I try appending anything to the rule e.g:

RewriteRule ^(.*/) /index.php/hello [PT,L]

I get a 500 internal server error. How do I get this working?

+1  A: 

Try changing your RewriteRule to

RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]

That will rewrite the URL to one that includes the subdomain and the original request URI.

EDIT: maybe it needs to be

RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]

as mentioned in the comments.

David Zaslavsky
I tried this but it doesn't appear to be firing, even when I change index.php to something else.
Zahymaka
Oh... maybe you need to use the pattern ^(.*)$ (no slash) when it's in .htaccess. (For what it's worth, I would recommend putting these directives in the main server configuration file if you can.)
David Zaslavsky
See below. Thanks for all the help!
Zahymaka
+1  A: 

This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;

RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.example$ [NC]
RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]
MatW
This works, but I don't want to redirect users on the site. Thanks all the same.
Zahymaka
I'm not in a position to test it right now, but would it not work just the same by replacing [R=301,L] with [PT,L]?
MatW
+1  A: 

You probably need to exclude the index.php from your rule:

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
Gumbo
Thanks, I used something else (-f and -d), but this works as well, so I'm going to file it away for future reference.
Zahymaka