.htaccess Wildcard Subdomains

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

http://subdomain.example.com is displayed at http://example.com/index.php/accounts/subdomain/ . My rules look something like this:

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 ignores everything else. When I try to add something to a rule, for example:

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

I get 500 internal server errors. How to do it?

+2
source share
3 answers

You probably need to exclude 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] 
+3
source

Try changing the RewriteRule to

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

This will rewrite the URL for one, which includes the subdomain and the original URI request.

EDIT: maybe it should be

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

as stated in the comments.

+1
source

This is an adaptation of the code that I use to redirect subdomains on my own site. I do not claim to be the 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] 
+1
source

All Articles