Friday, January 14, 2011

how to get subdomain to link with folder?

I am running xampp which has Apache on windows for a dev server. I need to use a subdomain of localhost to access my localhost/images/ folder by going to h##p://images.localhost

However I am having trouble, I have posted an image below showing my problem.

So the question is how to you setup a subdomain on Apache and actually have it work for a folder like I need?

Please view this image, sorry new users can't post more then 1 url and NO images so here is my 1 url to an important image

http://www.freeimagehosting.net/uploads/b0194b8e68.jpg

UPDATED VERSION

My apache conf file

NameVirtualHost localhost:8080

<VirtualHost localhost:8080>
   ServerName localhost
   ServerAdmin blah@blah.com
   DocumentRoot c:\server\htdocs
</VirtualHost>

<VirtualHost images.localhost:8080>
   ServerName images.localhost
   ServerAdmin blah@blah.com
   DocumentRoot c:\server\htdocs\images
</VirtualHost>


My windows host file

127.0.0.1 images.localhost
127.0.0.1 *.localhost
  • I would imagine that your problem is trying to do a subdomain of "localhost". Best thing to do would be to change your servernames to something like "localdomain.com" and "images.localdomain.com" (anything will do really), and then modify your hosts file to map that domain to 127.0.0.1

    Been a while since I've used windows (sorry), but I believe the hosts file is in c:\windows\system32\etc\hosts

    And the format for the fake domain would be something like:

    localdomain.com     127.0.0.1
    

    Then, you'd just need to bounce your XAMPP server and everything should be good to go.

    Hope that helps :)

    womble : I'm sure the actual owner of localdomain.com (or whatever actual domain the OP chooses as a result of your bad advice) will be thrilled.
    jasondavis : Actually I have a domain for the site it's just not hosted anywhere so I will try this with my domain and see if it works
    From Ian Selby
  • Configuration looks fine to me. Check for simple issues like restarting apache after changing configuration file, etc.

  • As described in the RFC 2606 .localhost. is treated special.

    But one other thing to look at: What is your default directory? It could be that non of your virtual settings are working properly. But instead the DocumentRoot of the server itself is always handling the requests.

    jasondavis : my doc root is c:\\windows\server\htdocs\
  • The problem is that 'localhost' always resolves to 127.0.0.1 but subdomains of localhost such as your 'images.localhost' are not defined and therefore are not resolving. You can correct this locally by editing your system's hosts file (usually in c:\windows\system32\drivers\etc) and adding the following line:

    127.0.0.1 images.localhost
    

    You could also add:

    127.0.0.1 *.localhost
    

    After saving the hosts file, your subdomains should resolve correctly.

    Edit: I see that you have the vhosts configured to listen on port 8080 but your URLs don't include the port number. You need to browse to the addresses with :8080 in them like this:

    http://images.localhost:8080/layout/homepage/welcome_image.jpg
    

    Alternatively, you can change the vhosts to listen on :80.

    I'm guessing the reason just localhost works is that your primary Apache conf has a Listen 127.0.0.1 and DocumentRoot c:\xampp\htdocs.

    jasondavis : Did you see the image in my post? I have all this done and nothing seems to work, I even tried using a real domain name in my hosts file and my computer will just timeout trying to load it, I have restarted apache and even rebooted my PC with no luck yet
    Dave Forgac : Did specifying the port number in your request work for you?
  • I even tried using a real domain name in my hosts file and my computer will just timeout trying to load it

    Maybe you have a problem with name resolution. You can try "ping images.localhost" from command line (Start -> Run -> "cmd")

    If that subdomain isn't working, maybe you can try with a real one. My last article might help: 42foo: all the virtual hosts you need for your web development

    I have this working on some domains, although I link them all to the same document root:

    <VirtualHost *:80>
            DocumentRoot /srv/apps/mydomain/current/public
            ServerName mydomain.com
            ServerAlias www.mydomain.com
    </VirtualHost>
    
    <VirtualHost *:80>
            DocumentRoot /srv/apps/mydomain/current/public
            ServerName assets0.mydomain.com
            ServerAlias assets1.mydomain.com
            ServerAlias assets2.mydomain.com
            ServerAlias assets3.mydomain.com
    </VirtualHost>
    
  • The problem, as far as I understand Apache virtual hosting anyway, is that you're using a catch-all in your Virtual hosts config and using a mutual DocumentRoot, rather than explicitly defining each VHost's name and DocumentRoot:

    *:8080 is your base NameVirtualHost setting.

    This will catch all requests to :8080, and it will redirect all requests to the virtual host that matches the directive.

    It becomes problematic when you have two Vhost directives that are named the same thing, and one of the directive's DocumentRoot matches the server's DocumentRoot. If there is no "Overriding" DocumentRoot that the server has separately, then Apache will evaluate each Vhost's DocumentRoot, finding the "Occam's Razor" of the values it encounters if multiple Vhost's share a root path.

    In this case ...\htdocs is the DocumentRoot for both, because ...\images is contained within ...\htdocs. Thus any requests will automatically default to the Vhost that offers only ...\htdocs as its DocumentRoot.

    I realize that was a bit confusing, so to fix this: Switch to name based virtual hosting.

    UPDATE (2009-08-25)
    There's something I need to clarify before we move on:

    Apache and other web servers never listen on :8080 by default. Also, web clients like Firefox never make a request to :8080 by default. I assumed that you understood that from your original post, since your VHost directive showed a non-standard port of :8080. Now, I'm not so sure that was clear.

    In order for the previous revision of my post to have worked for you as it was setup (without port 80 redirects or what have you), you would need to specify the port when requesting a page:

    http://localhost:8080 and http://images.localhost:8080

    I should have included that information. As I said, I assumed that was clear from your original post. My apologies for that. By extension, I also assumed you were setting it up this way because another server was listening on port 80. If that's true, then you might consider combining the two servers into one setup, or turn off the alternate while you do your work with xampp.

    So, let's correct the VHost directives to listen to port 80, which is the default port that pages are served from, and requested from, by servers and clients respectively.

    I'm also going to get anal retentive about defining the localized permissions for the folders that you're serving data from, since I'm worried that there is a / Directive that is munging your ability to get pages from your sites.

    UPDATED VirtualHost Directives (2009-08-25):

    NameVirtualHost localhost:80
    
    <VirtualHost localhost:80>
       ServerName localhost
    
       # Naturally, this can be changed to a real email.
       ServerAdmin blah@blah.com
    
       # Set our DocRoot for the VHost.
       DocumentRoot c:\xampp\htdocs
    
       # Define access perms for our DocRoot.
       <Directory "c:\xampp\htdocs">
         # We're going to define Options, Override perms, and Allow directives.
         # FollowSymLinks probably doesn't work in Windows, but we'll keep it for posterity.
         Options Indexes MultiViews FollowSymLinks
    
         # Disallow Override
         AllowOverride None
    
         # Setup 1. Allow only *from* localhost. Comment out the following 3 lines, 
         # and uncomment Setup 2 below to allow access from all.
         Order deny,allow
         Deny from all
         Allow from 127.0.0.0/255.0.0.0
    
         # Setup 2. Allow all. Uncomment the following 2 lines, and comment out Setup 1.
         #Order allow,deny
         #Allow from all
    
       </Directory>
    </VirtualHost>
    
    <VirtualHost images.localhost:80>
       ServerName images.localhost
    
       # Naturally, this can be changed to a real email.
       ServerAdmin blah@blah.com
    
       # Set our DocRoot for the VHost.
       DocumentRoot c:\xampp\htdocs\images
    
       # Define access perms for our DocRoot.
       <Directory "c:\xampp\htdocs\images">
         # We're going to define Options, Override perms, and Allow directives.
         # FollowSymLinks probably doesn't work in Windows, but we'll keep it for posterity.
         Options Indexes MultiViews FollowSymLinks
    
         # Disallow Override
         AllowOverride None
    
         # Setup 1. Allow access only from localhost. Comment out the following 3 lines, 
         # and uncomment Setup 2 below.
         Order deny,allow
         Deny from all
         Allow from 127.0.0.0/255.0.0.0
    
         # Setup 2. Allow all. Uncomment the following 2 lines, and comment out Setup 1.
         #Order allow,deny
         #Allow from all
    
       </Directory>
    </VirtualHost>
    

    Apache will get grumpy about this setup if there is no corresponding DNS name that it can find for a virtual host, so the recommendation earlier about modifying the systems's hosts file is still accurate. Note that when you edit the hosts file, you can put all the aliases for a single IP on the same line, saving you some confusion.

    127.0.0.1   localhost   images.localhost
    

    You can also do IP based Vhosting but I wouldn't recommend it. It's much more involved than what you're doing and is only "really" necessary when you're dealing with multiple vHosts using SSL.

    Anyway, the setup I've described works exactly as expected on my system (Ubuntu 8.10 x86_64, Apache 2.2.9) and should also work fine on yours.

    jasondavis : I added in your changes but it had no affect on my system for some reason, I posted my updates in my post above if you care to look, thanks
    Sean Lewis : Just in case there's no notification on updating an entry, I updated the entry with some additional information that will (hopefully) fix your problem.
    From Sean Lewis

0 comments:

Post a Comment