1

I've been trying to accomplish this for hours now and for some reason it does not want to work.

All I want is username.domain.com to internally go to domain.com/users/username

I set-up my dns that the wildcard subdomain points towards public_html/wild folder and I placed an index.html file in there and all seems to work well.

The problem is when I use this .htaccess file:

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/users/%2%{REQUEST_URI} [PT,L]

It shows a 404 page not found error. I checked the logs and it says it still looks in the /wild folder, however my script is one level up in the public_html folder.

How can I fix this problem? Thank you!

user1993382
  • 87
  • 1
  • 9

2 Answers2

0

You say you've pointed the wildcard subdomain to public_html/wild, which means when someone requests http://user.domain.com/ the / request is mapped to public_html/wild. This means a request for the wildcard subdomain will never be able to get mapped to anything in the parent directory public_html (that's why it's called a document root).

One of the things you can do is point the document root of the wildcard subdomains to the same place as the main domain, the place where index.php is. But if you've got content from the main domain that you don't want to get mixed into the subdomains or vice versa, you could try using the P flag which utilizes mod_proxy and creates a reverse proxy:

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^index\.php($|/) http://www.domain.com/index.php/users/%2%{REQUEST_URI} [P,L]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Hi, thank you for helping, however I get the following error: Proxy Error The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /users/www/users/www/users/www/users/www/users/www/users/www/users/myusername/. Reason: DNS lookup failure for: www.domain.com Additionally, a 502 Bad Gateway error was encountered while trying to use an ErrorDocument to handle the request. – user1993382 Apr 18 '13 at 08:02
  • In addition, if it matters, I am using CodeIniter, but everything is setup properly as domain.com/users/username works just fine. – user1993382 Apr 18 '13 at 08:03
  • Also, I'm assuming this .htaccess file belongs in the wild folder and not the public_html folder as there is one there as well, it's simply removing the index.php file for clean codeigniter urls. – user1993382 Apr 18 '13 at 08:06
  • @user1993382 accidentally forgot the first condition, bad copy/paste – Jon Lin Apr 18 '13 at 08:11
  • thank you, however it still points towards domain.com/wild instead of just domain.com and I still get a 404 not found error. If i remove the end of the uri and just leave the domain on that last line it loads the domain/wild folders index file that i placed there. is there a way for me to remove that wild from there and have it go straight to my main url? Thank you! – user1993382 Apr 18 '13 at 08:19
0

Here is what finally worked:

RewriteCond %{HTTP_HOST} ^([^.]+)\.myserver\.com$ [NC]
RewriteRule (?!^m/$)^.*$ http://myserver.com/m/sites/%1%{REQUEST_URI} [NC,L,P]
user1993382
  • 87
  • 1
  • 9