HTAccess Question

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.

cookie,

Sorry, you cannot create a subdomain via .htaccess because this is a function of the DNS daemon on the server and redirections are handled there (normally to the domain/subdomain directory).

Then, to use a URI like filename/garbage, you must enable MultiViews (which, IMHO, causes more problems than it’s worth).

I see that you’re using !www as your “escape” from the loop which you’d generate with the (.*). While I give you credit for knowing how to escape the loop, IMHO that’s a poor technique. It would be far better to know the characters you allow in the atom and specify them.

Finally, the Last flag does not mean to ignore any subsequent rules. It means that any subsequent rules are not ANDed with the rule set terminated by the Last flag.

Regards,

DK

Hi dklynn,

Thank you for taking the time to reply.

Sorry, you cannot create a subdomain via .htaccess because this is a function of the DNS daemon on the server and redirections are handled there (normally to the domain/subdomain directory)
Yes, however I can create a virtual subdomain (a subdomain that doesn’t actually exist). All that I want are re-directs to specific controllers.

I’m afraid my knowledge of Apache and htaccess is poor at best. Could you elaborate on how I could ignore subsequent rules? i.e. Stop evaluation once

RewriteRule ^index.php/(.*)$ http://www.example.com/index.php/%1 [P,L]

is reached?

Again, thank you for the reply. Apologies if my questions are absolutely noobish :slight_smile:

cookie,

If your DNS is setup to accept wildcard subdomains, you’re correct about being able to “create a virtual subdomain” otherwise, it is not possible because your subdomain, if it doesn’t exist, cannot receive a request. Subdomain requests do not get delivered to the main domain when the subdomain does not exist, i.e., think of each subdomain as a completely different (and separate) domain and you’ll understand this important point.

As for ignoring mod_rewrite directives, it can only be “hardwired” by a RewriteEngine off (go into comment mode) OR by failing to match any mod_rewrite directives. Okay, the Next flag may be what you’re after as it will cause mod_rewrite to immediately execute the redirection AND start the next iteration - mod_rewrite will not stop until it fails to match on a pass through all its code. IMHO, that is a feature, not a fault of this fine module.

No apologies for “noobish” as the only question which is silly is the one not asked. We’ve all learned here whether by reading others’ questions, asking our own questions or answering questions (I’ve learned by responding to member questions). Keep asking as long as you have questions!

Regards,

DK