Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mod-rewrite redirect doesn't work

Do I need a DNS entry too?



3:46 pm on May 7, 2002 (gmt 0)

Inactive Member
Account Expired



I make apache 1.3.24 with all module on a RH7.2.
I want to redirect the url www.server.com/~user to www.user.server.com
so i write in httpd.conf :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.server\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1
RewriteRule ^www\.([^.]+)\.server\.com(.*) /home/$1/www/$2

and I have

UserDir /home/*/www

<VirtualHost xxx.xxx.xxx.xxx>
ServerAdmin webmaster@serveur.com
DocumentRoot /home/xxx/www
ServerName www.serveur.com
ServerAlias serveur.com
CustomLog logs/serveur-access_log combined
ScriptAlias /cgi-bin/ /home/xxx/cgi-bin/

but when I go to www.user.server.com apache display the document root of the server.
I think I must have a DNS entry in the DNS server for www.user.server.com
But what is the file and the line to write ?
Thanks for your responses and excuse me for my bad english ;)


6:05 pm on May 9, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 21, 1999
votes: 0

Welcome to wmw fanzila, we're pleased to have you join us!

I can do a few tricks with mod_rewrite but your question is beyond my meager talents. I am making an effort to find an expert or two that can help you so please be patient! :)

7:48 pm on May 9, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 10, 2001
votes: 10

I'm not exactly sure which way you're trying to go here.

Your DNS question actually makes me think that you might be confusing internal rewrites (done transparently by the server) and external redirects (executed client side by the browser).

Using the %{HTTP_HOST} in the subsitution part of a RewriteRule implies a browser redirect, which would need the [R] flag. Adding the host name to the path for the server in an internal rewrite serves no useful purpose.

There's also the problem that the string you're trying to match with the next rule actually only contains the path component of the URL. Your "^www\.([^.])+\.server\.com(.*)" pattern will therefore never match. To get at the user part of the request you can use a %# type reference to a group in the last RewriteCond pattern instead.

So, what I would try looks something like this (untested):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.server.com$ [NC]
RewriteRule ^(.*)$ /home/%1/www/$1 [L]

The [NC] flag for the RewriteCond makes the matching case-insensitive. This *may* lead to trouble later, because your user directories are case sensitive. If you don't use this flag, then any visitor specifying "...COM" instead of "...com" will get past this ruleset, probably seeing your document root again. You could try to use eg. the "tolower" mapping function, but I'm not sure if I understand its use correctly yet. Something like /home/${tolower:%1}/www/$1 might do the trick.

The [L] flag for the RewriteRule stops mod_rewrite processing at that point. This saves some CPU time, but if you have other rules further down that are supposed to modify the result of this one, then you'd have to remove it.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members