Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

.htaccess and working with subdomains locally

Hey everyone,

I am trying to set up working with subdomains locally on a Mac. I have MAMP Pro, and have my project set up as "domain.dev". What I'm attempting to do is go to "subdomain.domain.dev" and have it point to "domain.dev" so the URL doesn't change. "subdomain" can have any title, I'm not trying to just search for the title of one specific subdomain, but for several.

I've tried some rewrite rules and conditions to no avail. With my editing the host file to point to "subdomain.domain.dev" as local host (because MAMP doesn't allow wildcards), anything I put in the subdomain part of it just brings up an "Index Of" of all my project/folders. And if I comment out that pointer, the url doesn't load at all.

Here's what I have so far:

RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.domain\.dev$ [NC]
RewriteRule ^(.*)$ $1.domain.dev/ [L,R=301]

If anyone could help or give me some insight as to what I'm doing wrong or if this is even possible, it'd be appreciated. Thanks! Also tagging Randy Hoyt

-Mike

3 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It is possible. Three comments:

(1) Right now, it looks like your rule is only getting applied if the domain is domain.dev or www.domain.dev. That's what the RewriteCond is saying, but that's not when you want it to get applied.

(2) Once you fix the RewriteCond, then I don't think you want the [R=301] flag on that second rewrite rule; that would mean that the web address should be redirected instead of just rewritten.

(3) Once you fix the RewriteCond, then you have a slight bug in it. It will redirect something like this ...

subdomain.domain.dev

... to this ...

subdomain.domain.dev.subdomain.dev

... and then I expect that it will redirect that to this ...

subdomain.domain.dev.subdomain.dev.domain.dev

... and then I expect that it will redirect that to this ...

subdomain.domain.dev.subdomain.dev.domain.dev.domain.dev

Take a look at the two pieces again, the pattern to match and the result.

Hope that helps!

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

Hi Randy Hoyt,

So I've been playing around with this Rewrite Cond/Rule and I'm thinking something is quite possibly wrong.

Going off of your first point, that the RewriteCond is getting applied to domain.dev or www.domain.dev, I started over and am trying to work my way to where I need my regex to be.

So I started with this to test if the redirection was working at all:

RewriteCond %{HTTP_HOST} ^domain\.dev$ [NC]
RewriteRule ^(.*)$ http://www.google.com/

That worked. However, from what I've been picking up with regular expressions, I'm under the impression that this:

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.dev$ [NC]

should match any subdomain attached to the main site. If that's the case, in theory, subdomain.domain.dev should redirect to google.com. Right? Instead, I'm just getting the index page of all my projects in my main development folder when I go to subdomain.domain.dev.

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

I got it!!

Turns out it wasn't an issue with the htaccess file. I tried

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.dev$ [NC]

on my live site and it worked as I expected. So logically, it had to be the way my local environment was set up. Just by adding a subdomain type url in the hosts file didn't seen to do the trick, and through MAMP PRO, you can't edit the apache httd.conf file directly because it gets rebuilt every time you start up the server. So I added this line

ServerAlias *.MAMP_VirtualHost_ServerName_MAMP

to the template that MAMP PRO works off of and now the subdomains load properly the way I need them to. Thank you Randy for your guidance!

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Well done! Glad I could help with getting the htaccess file correct.