Forum Moderators: phranque
I am trying to redirect:
"user1.mydomain.com" to "user1.mydomain.com/user1"
and
"user2.mydomain.com" to "user2.mydomain.com/user2"
and
"user3.mydomain.com" to "user3.mydomain.com/user3"
and so on......
-I am running my own Apache 2.0.48 web server. I have access to httpd.conf and .htaccess.
-I have set up the user folders in htdocs.
-I have set up all the DNS host records at my DNS service provider for each of the users (i.e. "user1.mydomain.com" points to my IP address).
-I know the HTTP_HOST is registering correctly because when I run phpinfo() is see all the referers.
-I have mod_alias and mod_rewrite loaded in apache and I've restarted apache everytime I made a change to anything. One thing to note: When I try to start apache with mod_alias commented out it wont restart, don't know why. The reason I'm trying to comment it out is because I read somewhere that if both mods are loaded I might need to use the [PT] flag, but don't know how to use the flag, doesnt tell me anywhere that I've looked anyways.
-I've tried to set up a virtual host in httpd.conf
-I've tried mod_rewrite and mod_alias redirects and rules in both httpd.conf and .htaccess.
Nothing is working for me and obviously, I just don't get it and I guess I need help.
Here's some code that I got so far from somewhere (can't remember where, it's been a crazy 2 days):
RewriteEngine on
#RewriteMap lc int:tolower
RewriteCond %{HTTP_HOST}!\.mydomain\.com$ [NC]
RewriteRule ^(.+) - [L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule ^(.+) - [L]
RewriteCond %{HTTP_HOST} ^[^.]+\.mydomain\.com$ [NC]
RewriteRule ^(.+) %{HTTP_HOST}=$1 [C]
RewriteRule ^(.*)=(.*) ${lc:$1}$2 [C]
RewriteRule ^([^.]+)\.mydomain\.com(.*) [$1.mydomain.com...] [R,L]
(if I try to uncomment #RewriteMap lc int:tolower, apache won't restart)
I've also tried:
redirect Permanent user1\.mydomain\.com [user1.mydomain.com...]
So...hopefully that's enough to go on for some kind soul out there that can find the time to help a guy out. It would be much appreciated.
Thanks
Mike B.
Welcome to WebmasterWorld [webmasterworld.com]!
I don't see anything really wrong with what you've got, so I just sat down and rewrote it from scratch as an exercise. You can compare the code and the results if you like.
I assume the main point here is to prevent problems if a user enters his/her name using uppercase or mixed-case, but my first example omits the case-conversion for simplicity. In the second case, I included the requested resource in the case conversion as well - It will eliminate *any* uppercase letters in the request.
Here's what I came up with:
Without lowercase subdomain-name conversion:
RewriteEngine on
RewriteCond %{HTTP_HOST} !\.mydomain\.com [NC]
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule .* - [L]
#
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com [NC]
RewriteRule ^/(.*) http://%1.mydomain.com/%1/$1 [R=302,L]
With lowercase subdomain-name conversion:
RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{HTTP_HOST} !\.mydomain\.com [NC]
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule .* - [L]
#
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com [NC]
RewriteRule ^/(.*) ${lc:%1/$1} [C]
RewriteRule ^([^/]*)/(.*) http://$1.mydomain.com/$1/$2 [R=302,L]
Now, you might want to use a completely-internal rewrite, so that the user sees only "his own" domain name, and the slow external redirect with subsequent second HTTP request is avoided:
RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{HTTP_HOST} !\.mydomain\.com [NC]
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule .* - [L]
#
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com [NC]
RewriteRule ^/(.*) ${lc:%1/$1} [C]
RewriteRule ^([^/]*)/(.*) /$1/$2 [L]
The above code is not tested; There may be a slash or dash out of place. Note that I left the end-anchors off your hostnames. End-anchoring can cause unexpected problems if a proxy forwards a port number appended to the hostname, i.e. http://user.mydomain.com:80
Note also that "%1" back-references the first parenthesized subgroup in the preceding matched RewriteCond pattern. I used it to "copy" the subdomain name above, which eliminated one chained rule. Back-reference %1 works the same as $1, but refers to the preceding RewriteCond pattern instead of the RewriteRule pattern.
I also omitted several other start anchors, parentheses, and end-enchors where they were not needed -- For example, end-anchors following ".*" or "(.*)".
If you don't really need the lowercase enforcement feature, you can also do a user-subdomain redirect by using the RedirectMatch directive of Apache mod_alias -- It will allow you to use a back-reference to copy the subdomain name to the subdirectory path, unlike the Redirect directive, which does not support back-references. You will end up needing a "www" subdirectory, though, since RedirectMatch's operation is unconditional.
Anyway, you can compare this code to what you've got if you want, and see if if works any better or worse -- or differently.
Jim
[edited by: jdMorgan at 11:14 pm (utc) on Feb. 21, 2004]
It is not important at all about the upper or lower case, it just happenned to be included in some code I found in a forum (it may have even been your code, not sure). I just thought I'd include it in my question in case it was important.
I tried your first section of code and it still didn't work. I put it in the <Directory "...../htdocs"> container (right after the DocumentRoot section of the httpd.conf file). Is that right? I am really new to this and I'm not even sure where I am supposed to be putting this code. Sorry.
Can I ask for an example of what you meant in your last statement regarding the RedirectMath (I tried that as well and it didn't work...again, maybe I'm not putting it in the right place - i.e. httpd.conf, .htaccess , which container, etc....). Also, when you say I need a www directory, do you mean instead of htdocs, or under htdocs? Sorry 'bout my newbiness.
Thank you so much! I really appreciate the help.
RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{HTTP_HOST}!\.mydomain\.com [NC]
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule .* - [L]
#
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com [NC]
RewriteRule ^/(.*) ${lc:%1/$1} [C]
RewriteRule ^([^/]*)/(.*) /$1/$2 [L]
into my .htaccess file and i got an internal server error. I went back and commented out #RewriteMap lc int:tolower , and the error went away. I must have something configured wrong because that is what stopped apache from starting before.
How can I tell if my mod_rewrite is loding correctly (it is uncommented in httpd.conf)
Thanks again.
Mike B.
I can't help much with httpd.conf - I rent server space, and don't have access to it myself.
You will also find subtle differences between mod_rewrite in httpd.conf and .htaccess contexts. For example, the leading slash is stripped off the requested URI as seen by RewriteRule. So in httpd.conf, you'd write:
RewriteRule ^/index\.html$ /index.htm [L] RewriteRule ^index\.html$ /index.htm [L] In order to enable mod_rewrite, you will need AllowOverride All or at least AllowOverride FileInfo Options in httpd.conf.
Then you will need Options All or at least Options FollowSymLinks in httpd.conf, or Options +FollowSymLinks in .htaccess before using any mod_rewrite directives.
Both AllowOverride and Options directives are described in the Apache core [httpd.apache.org] directives documentation.
If you haven't done this before, then by all means, start with a simple rewrite, like:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /index.html [L]
in .htaccess. With this code, if you request "silly.html" with your browser, your server should return your index.html page instead. The file silly.html need not exist. RedirectMatch is documented as part of Apache mod_alias [httpd.apache.org]. The problem I mentioned is this: RedirectMatch does not have the "brains" to *not* redirect "www.mydomain.com" to the subdirectory, "www.mydomain.com/www/" -- It will redirect all subdomains unconditionally. So you will have to have a www subdirectory to support that.
Jim
Thanks for all your help jdMorgan. You are the best!
So...it turns out that I was putting the code in the wrong section of my httpd.conf file. I was putting it in the main server container whereas I should have been putting it in a virtual host container.
Here's what is working for me (for anyone else who is struggling with this):
<VirtualHost *:80>
RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{HTTP_HOST}!\.mydomain\.com [NC]
RewriteRule .* - [L]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule .* - [L]
#
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com [NC]
RewriteRule ^/(.*) ${lc:%1/$1} [C]
RewriteRule ^([^/]*)/(.*) /$1/$2 [L]
</VirtualHost>
Thanks again.
Mike B.