2

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?

Zahymaka
  • 6,523
  • 7
  • 31
  • 37

3 Answers3

3

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
  • 643,351
  • 109
  • 780
  • 844
  • 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 Jun 29 '09 at 21:48
1

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 Z
  • 128,184
  • 27
  • 255
  • 279
  • I tried this but it doesn't appear to be firing, even when I change index.php to something else. – Zahymaka Jun 29 '09 at 18:32
  • 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 Z Jun 29 '09 at 19:47
1

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]
Mathew
  • 8,203
  • 6
  • 37
  • 59
  • This works, but I don't want to redirect users on the site. Thanks all the same. – Zahymaka Jun 29 '09 at 18:27
  • 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]? – Mathew Jun 29 '09 at 18:40