ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[SOLVED] htaccess: Subdomain to template_group

July 03, 2011 8:30pm

Subscribe [4]
  • #1 / Jul 03, 2011 8:30pm

    Wouter Vervloet

    758 posts

    Hi Guys,

    I’ve been struggling with this mod_rewrite thing for a while now, so it was time I threw it into the (awesome) group.

    I have a site where some of the sections (pages) are accessed through subdomains (for example: blog.domain.com, news.domain.com, etc..) and also just the normal domain (domain.com). All the subdomains need to point to a template_group and I’ve almost got it working.

    This is the .htaccess I’ve got so far:

    RewriteEngine on
    
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^(blog|news).domain.com$ [NC]
    RewriteCond %{REQUEST_FILENAME} !(blog|news)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ %1/$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

    Like I said… it almost works, except for this one little problem. When I access an url like blog.domain.com/entry/ee-is-awesome it works as expected and loads the blog/entry template. But when I go to blog.domain.com it just shows me the same template as domain.com instead of loading blog/index. Even if I add some jibberish at the end (blog.domain.com/sdffds) it works and I get the blog/index template…

    Who here has enough mod_rewrite chops to help me out on this one, because I’m stumped.

    Regards,
    Wouter

  • #2 / Jul 04, 2011 12:31pm

    GDmac - expocom

    350 posts

    i use used to use two htaccess files, one in the subdir
    http://ellislab.com/forums/viewthread/163582/#800306

    UPDATE
    i tested this, and your idea seems to save me a ton of work instead of using subdirs.
    (if you point the subdomain to the same dir, assets stay the same too e.g. /images etc.)

    #empty request ^$ to subdomain e.g. test.example.com
    RewriteCond %{HTTP_HOST} ^(blog|test).example.com$ [NC]
    RewriteRule ^$ index.php?/%1/ [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !\.(jpe?g|gif|png|bmp|css|html?|pdf)$
    RewriteRule ^(.*)$ /index.php?/$1 [L]
  • #3 / Jul 04, 2011 1:41pm

    Wouter Vervloet

    758 posts

    Hi Gerhard,

    Your example didn’t work for me, but… it did solve it for me 😊 The aha-moment for me was when I saw this part:

    RewriteRule ^$ index.php?/%1/ [L]

    It occured to me that my RewriteRule didn’t pass when it had no first segment. So when I added that, I got it to work with no first segment, but then it didn’t work when I added /archive or /search to the URL.

    So I added my rules back in and got it to work perfectly. This is the final (bloated) .htaccess file:

    RewriteEngine on
    
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^(blog|news).domain.com$ [NC]
    RewriteRule ^$ %1/ [L]
    
    RewriteCond %{HTTP_HOST} ^(blog|news).domain.com$ [NC]
    RewriteCond %{REQUEST_FILENAME} !(blog|news)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ %1/$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

    Just a little heads-up: when using this method, you have to keep in mind that your first segment is actually your second and your subdomain becomes your first segment.

    // <a href="http://blog.domain.com/entry/test-entry-1">http://blog.domain.com/entry/test-entry-1</a>
    {segment_1} => blog
    {segment_2} => entry
    {segment_3} => test-entry-1

    Thanks for the help!

    Regards,
    Wouter

  • #4 / Sep 12, 2011 8:48pm

    Carlos Perez

    13 posts

    Wouter - may I ask if all your permalinks and such properly show up as blog.domain.com/blog-post-name-here?  Or does it still come up as http://www.domain.com/blog/blog-post-name-here?

    Cheers,
    Carlos

  • #5 / Sep 12, 2011 9:06pm

    Wouter Vervloet

    758 posts

    Hi Carlos,

    The links generated by EE are all a bit messed up and look like domain.com/blog/blog-post-name-here/

    I worked around this by setting up global variables for all the subdomains I have and manually putting the URL’s together. It’s not ideal, but the client wanted it like this. If I knew it would be such a PITA I would’ve advised against it or done it a different way (MSM or something like that).

    —Wouter

  • #6 / Sep 12, 2011 9:09pm

    Carlos Perez

    13 posts

    Ah got it… that’s too bad. Thanks for letting me know before I spent hours pulling my hair out about it! 😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases