Forum Moderators: phranque
I'm interested in setting up a situation where someone who enters dira.domain.com goes to my directory at domain.com/dira, entering dirb.domain.com goes to my directory at domain.com/dirb, etc. I'm using an apache server. If the directory does not currently exist, it should forward to my home page, or better yet a 404. It would also need to show dira.domain.com in the browser's url, and not domain.com/dira.
I know I'll have to set up BIND to have a virtual directory, ie: *.domain.com.
I just need to know what kind of code that I need to put in my apache config file to make it happen, so that whatever directory I currently have can be accessed via directory.domain.com. I also don't want to hard code each directory name, if possible.
Also, this would only need to apply for one domain, and not all of the rest of the domain names hosted on this server.
I've read the apache docs till I'm blue in the face, to no avail. Please help if you know how. Thanks in advance!
Welcome to WebmasterWorld!
See Apache mod_rewrite, specifically, use RewriteCond testing %{HTTP_HOST}, creating a back-reference to the subdomain, and then use it in a RewriteRule to rewrite to the appropriate subdirectory.
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim
Then, others searching the internet for the same solution would finally find the answer. Here ... at this forum. Then you would be even more famous!
.. anyone?
Beware that code intended to work in .htaccess will need to be modified to work in httpd.conf.
Jim
In order for this to work, do I need to:
1) add a wildcard A record to BIND (*.domain.tld)
2) add a wildcard virtual domain (*.domain.tld) to my http.config file
3) then, add the code to my htaccess file at the same level as the subdomains?
Also, I have directories with dashes in them (ie: /like-this). Will the code that you tested take care of these types of directories?
Thanks in advance for your reply.
> 2) add a wildcard virtual domain (*.domain.tld) to my http.config file
Yes. Point it to your main root (main domain "home page") directory.
> 3) then, add the code to my htaccess file at the same level as the subdomains?
No. Based on #2 above, put the code in your main domain's "home page" directory. Or, you can put it into httpd.conf with some small changes.
I'm not sure what code you decided to try, so I can't answer your question about the hyphens. However, in most cases, this kind of rewrite does not need to check anything about the requested page URL-path, so it should work.
Jim
I'm getting the 403 forbiden error when accessing my site. I placed this code in a .htaccess file on my home directory, AS IS. Is there something else that I should also put before or after the code? I took special care to make sure there was a space before each! below.
# Rewrite subdomain requests to subdirectories except for www.example.com
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/sd_
RewriteCond %{HTTP_HOST}!^www\.my-domain\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.my-domain\.com
rewriterule (.*) /sd_%1/$1 [L]
#
# Redirect direct user-agent requests for www.example.com/sd_<subdomain>/<page> to [<subdomain>.example.com...]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sd_(.+)\ HTTP/
rewriterule ^sd_([^/]+)/(.*)$ [$1.my-domain.com...] [R=301,L]
Just to review, I have an A record in BIND set to *.example.com at my IP address.
I've added:
<VirtualHost [IP]>
ServerAlias *.example.com
DocumentRoot /home/example/public_html
ServerName www.example.com
</VirtualHost>
to my config file
With these 2 alone, I "should" be able to type something like a.example.com into a browser (a dir does not exist) and just get my example.com home page, but I get a 404 error instead.
Should I add something like:
ServerAlias *.example.com example.com www.example.com
to my original virtual host set-up, instead of adding a seperate virtual host for the wildcard domain?
When I check my example.com error logs and recent visitors, the hit to a.example.com doesn't show up anywhere.
Also to answer your question, when I added the above code (most recent post by me) to my htaccess file, I got the following error:
Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden:
So add
Options +FollowSymLinks
RewriteEngine on
Since www.example.com is defined as ServerName, there's no reason you should need to include it in ServerAlias as well. Just for neatness, you might want to put the ServerName ahead of the ServerAlias, though.\
Whenever you get an error --any error-- dig for the details and post them. On your 404 error, what URL is in the address bar? Is it the one you typed, or did it change?
Jim
It looks like the code I used (yours from previous page) adds sd_ to the actual directory names. I took the "sd_" out of the code and now the subdomain.url just directs me to my home page.
From what I'm seeing, the code is working, which is more luck than I've seen before, just not quite right yet.
I'll keep trying...
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
rewriterule (.*) /%1/$1 [L,P]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\ HTTP/
rewriterule ^([^/]+)/(.*)$ [$1.example.com...] [R=301,L]
The above is with the "sd_"'s extracted out.
My BIND A/config settings are now working. Requests for example.com go to my homepage, dira.example.com (actual virtual host set-up in apache config) goes to the correct subdirectory AND dummy.example.com (dummy is not an actual directory) goes to my default homepage.
But...
When I placed the above code in my .htaccess file, and requested the actual file example.com/s/test.php (s being actual dir), it gave me s.example.com/test.php, and 404 error that it did not exist. The browser's address bar is what address this showed. The error file said "/home/example/public_html/test.php" does not exist, which is correct because /home/example/public_html/s/test.php does.
I guess what I really need is to have a.example.com show a.example.com and read the content from example.com/a, but a direct request to example.com/a actually shows that address/content. Isn't this code what this would do?