Forum Moderators: phranque

Message Too Old, No Replies

Help with Virtualhost

         

stormshield

11:31 pm on Dec 26, 2006 (gmt 0)

10+ Year Member



Hi all,

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

jdMorgan

11:39 pm on Dec 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, and it's fairly easy. There are many posts related to this subject already here, one of them [webmasterworld.com] posted yesterday.

Jim

stormshield

1:12 pm on Dec 27, 2006 (gmt 0)

10+ Year Member



Hi 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

stormshield

6:47 pm on Dec 27, 2006 (gmt 0)

10+ Year Member



Well, on second thoughts I don't need it...

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

jdMorgan

6:50 pm on Dec 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that code works well on my servers for the application you describe, so it's probably some slight difference is setup. Please review your server error log and tell us what the server error message was when "it didn't work."

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

stormshield

7:34 pm on Dec 27, 2006 (gmt 0)

10+ Year Member



That's what error log says:

File does not exist: /home/httpd/html/storm/domain/graphics/right-round.jpg

The problem here is that the subdomain name between domain and graphics is missing. But how this could happen I have no idea... what about you Jim?

jdMorgan

7:57 pm on Dec 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, but I don't know how the code you posted works -- I didn't write it.

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

jdMorgan

8:03 pm on Dec 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To handle sub-subdomains such as "www.user1.example.com", I'd suggest:

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]

I would also suggest NOT using sub-subdomains like "www.user.example.com, but linking instead to user1.example.com instead. In this case, only the "www" subdomain is not mapped to a subdirectory. The URLs will be shorter, easier to remember, and take up less space in files and in IP packets...

Jim

stormshield

8:59 pm on Dec 27, 2006 (gmt 0)

10+ Year Member



OK, I'll try to explain exactly what I need.

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

jdMorgan

11:43 pm on Dec 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> max redirects reached

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]

The downside of this method is that you can't have a real subdirectory, or allow a username/subdomain, that starts with "sdd_".

Jim

[edited by: jdMorgan at 11:45 pm (utc) on Dec. 27, 2006]

stormshield

1:06 pm on Dec 28, 2006 (gmt 0)

10+ Year Member



I decided that creating a rule for each of my subdomains wouldn't be a bad idea, so now I have rules like:

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