Forum Moderators: phranque
I'm trying to write a virtualhost that points to a folder specified in subdomain, for example:
www.sub.domain.com TO /home/html/user/sub
www.computer.domain.com TO /home/html/user/computer
Can it be achieved with htaccess? (provided there already is a virtualhost directing all subdomains to /home/html/user/)
Any help highly appreciated,
Thanks
Storm
Jim
I've tried out the code and unfortunately it does not work - it yields a 500 Internal Server Error. Also, I wonder if this code will allow me to have separate htaccess? I think it won't, will it?
In case you don't get what I'm saying... I currently have this in virtual.conf:
<VirtualHost *>
ServerName www.domain.com
ServerAlias domain.com *.domain.com
DocumentRoot /home/httpd/html/storm/domain
</VirtualHost>
I would like every subdomain request to be processed to a respective folder without hand-coding.
Storm
I have another issue now. Look at the code:
RewriteCond %{HTTP_HOST} ^www.(.*).domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^(.*).domain.com$
RewriteCond %{REQUEST_URI}!^/(.*)/ #btw what is this line used for?
RewriteRule ^(.*)$ /%1/$1
This is working great as long as you don't request something like www.sub.domain.com/images/elephant.jpg.
Any ideas?
Thanks for your previous help,
Storm
The code is intended to go into .htaccess in your Web root folder, probably at /home/httpd/html/storm/domain if that is the directory in which your subdomain-subdirectories are to be created -- for example, if subdomain "user1.example.com" is to be mapped to /home/httpd/html/storm/domain/user1
If you currently have no other working rewriterules in that .htaccess file, you may need to add "Options +FollowSymLinks" or "Options -MultiViews +FollowSymLinks" ahead of "RewriteEngine on". Some server configurations require this Options directive, and some won't allow it (depending on AllowOverride settings in the config file), so I can't tell you whether to use it or not.
Jim
We're also going to need more information about what your links and URLs look like -- For example, you stated that you wanted to re-write subdomains, but you are apparently trying to rewrite both subdomains and sub-subdomains. Mod_rewrite code has to be specific and exactly correct in order to work. Please post some example links (using the example.com domain, such as www.example.com/log.image, user1.example.com/index.php, and www.user1.example.com/) and describe how these URLs are to be mapped to filepaths on your server.
I would not use the code you posted, as it is highly inefficient and contains several errors.
Jim
RewriteCond %{ENV:RewriteDone} !^Yes$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule (.*) /[b]%2[/b]/$1 [E=RewriteDone:Yes,L]
Jim
I'm creating a website with multiple subdomains, each of them pointing to a directory inside the root of the whole website:
www.sub.domain.com -> /home/httpd/html/storm/domain/sub
www.sub2.domain.com -> /home/httpd/html/storm/domain/sub2
www.sub.domain/graphics/icon.jpg -> /home/httpd/html/storm/domain/sub/graphics/icon.jpg
Non-www urls should be redirected to www urls
Some webistes might need a separate additional RewriteRule, for example:
www.sub2.domain.com/foo/variable1/variable2 -> www.domain.com/sub/foo.php?var1=variabl1&var2=variable2 (or whatever - really doesnt't matter)
And... well, it seems that's it.
Unless I'm much mistaken, your code should handle all that except for the last thing. Unfortunately, for some reason it triggers a [500] Internal Server Error.
mod_rewrite: maximum number of internal redirects reached. Assuming configuration
error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.
I hope I made at least a bit clearer... to be it looks like server issue - I hate my server:P
This implies that the "RewriteDone" environment variable logic isn't working, which is odd. Check the spelling and capitalization, as it must match exactly.
Another way to do this is to use a different method to prevent the infinite loop problem. Specifically, we can test the rewritten URL to determine if it has already been rewritten. This method requires you to uniquely-name your subdomain subdirectories; For example, map user1 to subdirectory "/sdd_user1". This allows the code to test to see if the current URL-path starts with "sdd_" and to only do the rewrite if it doesn't, thus preventing the loop:
RewriteCond $1 !^sdd_
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^(www\.)?sdd_[^.]*\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule (.*) /sdd_%2/$1 [L]
Jim
[edited by: jdMorgan at 11:45 pm (utc) on Dec. 27, 2006]
RewriteCond %{HTTP_HOST} ^(www\.)?foo\.domain\.com$
RewriteCond %{REQUEST_URI}!^/foo/
RewriteRule ^(.*)$ /foo/$1
RewriteCond %{HTTP_HOST} ^(www\.)?stuff\.domain\.com$
RewriteCond %{REQUEST_URI}!^/stuff/
RewriteRule ^(.*)$ /stuff/$1
I think it's the best solution for me... anyways, Jim, give a helping hand with creating yet another rule. I want this rule to redirect all non-www to www:
[foo.domain.com...] -> [wfoo.domain.com...]
[foo.domain.com...] -> [wfoo.domain.com...]
[domain.com...] -> [domain.com...]
Thanks for your help,
Storm