sub.domain.com/* -> domain.com/sub/* - rewrite

Using a single web hosting account to host multiple sites

Postby newbiewiz » Tue May 30, 2006 10:54 pm

hi Richard,

thanks again for the solution. its been working great so far.

i was wondering...

sub.domain.tld goes to domain.tld/sub
sub.domain.tld/somefile goes to domain.tld/sub/somefile

if i want anything.sub.domain.tld goes back to domain.tld/sub

is it possible?

cause now that i can achieve subdomain...

if people purposely go to www.sub.domain.tld , it will not lead them anywhere..

so is it possible to do something like a wildcard but for the subdomain ??

of coz this solution must be an addon to what you have proposed to me...
not a specific solution for a particular subdomain.

thank you :)
newbiewiz
 
Posts: 6
Joined: Thu May 25, 2006 8:01 pm

Postby richardk » Wed May 31, 2006 2:44 am

Just www.sub.domain:
Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.DOMAIN$ [NC]
RewriteRule .* /%2%{REQUEST_URI} [QSA,L]


Or any sub-subdomain:
Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^\.]+\.)?([^.]+)\.DOMAIN$ [NC]
RewriteRule .* /%2%{REQUEST_URI} [QSA,L]


You should only need to edit the "DOMAIN"s in both.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Uri » Wed May 31, 2006 8:16 am

Hi all,

I've tried the same but it's not working. I know mod_rewrite is running ok because I've tested it with a simple rule.

I suspect that there's a problem with the configuration with subdomains, because I don't get a server error, it's like the server doesn't exists (like when you type a fake domain), maybe it's denying subdomain access?

I'm using Apache 2.0.58 on Fedora, what should I tell my hosting provider to change in order to use this code?

Thanks!
Uri
 
Posts: 3
Joined: Wed May 31, 2006 7:52 am

Postby richardk » Wed May 31, 2006 8:30 am

Have you setup the DNS so the subdomains point to the right server? You might want wildcard DNS. You should look in your domain control panel or talk to your host/domain registrar.

You probably need server configuration aswell, the (sub)domain(s) not only have to be pointed to the right server, but they have to be pointed to the right place on the server (your main domain's document root, where the .htaccess file is). If the mod_rewrite doesn't work when the DNS is setup, you shoud ask your host about it.

If you're going to talk to your host, you should probably ask them about setting up <VirtualHost>s instead (possibly with mod_vhost_alias), as they are design for subdomain handling.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby thek-nar » Tue Jun 27, 2006 6:50 pm

richardk wrote:From: http://forum.modrewrite.com/viewtopic.php?p=4453#4453.
Code: Select all
RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.DOMAIN$ [NC]
RewriteCond %1---%{REQUEST_URI} !^(.+)---/\1(/.*)?$
RewriteRule .* /%1%{REQUEST_URI} [QSA,L]

Replace DOMAIN with your domain (without the www.).



hi
I tried this method to dispatch the user.domain.com/file to www.domain.com/~user/file on appache2

apache "userdir" module automatically create this ~user (alias?) for regular linux users

so I just added the "~" in the code
RewriteRule .* /~%1%{REQUEST_URI} [QSA,L]

http://user.domain.com/file works
http://user.domain.com/directory/ works
BUT FOR
http://user.domain.com/directory (without the slash / after directory)
the redirection seems to be explicit and the "~user" is somehow appended to the url in the browsers
so I have in my browser address : http://user.domain.com/~user/directory
(of course the redirection itself works I just don't want this ~user in the url)

how can I fix this
thanks!
thek-nar
 
Posts: 4
Joined: Tue Jun 27, 2006 6:27 pm

Postby richardk » Wed Jun 28, 2006 3:03 am

That's a known issue with mod_dir, this should sort it:

Code: Select all
RewriteEngine On

# subdomain code
RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.DOMAIN$ [NC]
RewriteCond %1---%{REQUEST_URI} !^(.+)---/~\1(/.*)?$
RewriteRule .* /~%1%{REQUEST_URI} [QSA,PT,L]

# mod_dir trailing slash fix
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$    [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN$ [NC]
RewriteCond %1---%{REQUEST_URI} ^(.+)---//?~\1(/.*)?$
RewriteRule ^~([^/]+)/(.*)$ /$2 [R=301,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby thek-nar » Wed Jun 28, 2006 12:03 pm

hi
thanks richardk for taking time to answer I really appreciate it

for some reasons the above solution does not work in my case

I have a this following VirtualHost config in appache:

#virtualhost for www.domain.com and domain.com
......

#virtualhost for users
<VirtualHost *:80>
DocumentRoot /randomdir
ServerName user.domain.com
ServerAlias *.domain.com
</VirtualHost>

the randomdir is a fake root dir, basically a empty dir with only a .htaccess file with the above code that internally redirect any request to to www.domain.com/~user
there is no .htaccess in the user actual public_html dir, is this trick ok ?
I think that once the redirection is made it does not use the .htaccess of this vitualhost anymore hence the # mod_dir trailing slash fix part is never parsed

I was thinking about another solution : before the # subdomain code
is it possible to make code that append the / if this is a directory and not if this is a file
for instance for all
user.domain.com/dir => user.domain.com/dir/
and
user.domain.com/file.ext => user.domain.com/file.ext

then the # subdomain code

I know this is not a good solution caus
user.domain.com/dir.sumthing
and
user.domain.com/file
woudn't work

but I'm ok with this limitation :-?
thek-nar
 
Posts: 4
Joined: Tue Jun 27, 2006 6:27 pm

Postby richardk » Wed Jun 28, 2006 12:41 pm

I was thinking about another solution : before the # subdomain code
is it possible to make code that append the / if this is a directory and not if this is a file
for instance for all

There's no real way of knowing if it's a file or a directory, we can guess by saying anything without a "." is a directory:
Code: Select all
RewriteEngine On

# add trailing slash if path does not include a "."
RewriteCond %{REQUEST_URI} !\.
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]

# subdomain code
RewriteCond %{HTTP_HOST} !^(www\.)?DOMAIN$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.DOMAIN$ [NC]
RewriteCond %1---%{REQUEST_URI} !^(.+)---/~\1(/.*)?$
RewriteRule .* /~%1%{REQUEST_URI} [QSA,PT,L]


Edit: or you could try removing this line
Code: Select all
RewriteCond %1---%{REQUEST_URI} ^(.+)---//?~\1(/.*)?$

from the "mod_dir trailing slash fix" code.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby thek-nar » Wed Jun 28, 2006 9:33 pm

richardk wrote:
Edit: or you could try removing this line
Code: Select all
RewriteCond %1---%{REQUEST_URI} ^(.+)---//?~\1(/.*)?$

from the "mod_dir trailing slash fix" code.


still no success is there some test u can advice to diagnose the problem?

richardk wrote:# add trailing slash if path does not include a "."
RewriteCond %{REQUEST_URI} !\.
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]


it works but I have an error after a few seconds when request http://user.domain.com
it works if I do http://user.domain.com/index.html
so it's a problem with the DefaultDocument loading although this is only a problem for the index.html in the root dir (http://user.domain.com/dir with default document works correctly)
thek-nar
 
Posts: 4
Joined: Tue Jun 27, 2006 6:27 pm

Postby richardk » Thu Jun 29, 2006 2:18 am

I don't think you should use mod_rewrite. I think you should try using mod_vhost_alias instead. If the paths to your users public_html directories were /home/<user>/public_html, you could use something like this:
Code: Select all
<VirtualHost *:80>
   ServerName user.domain.com
   ServerAlias *.domain.com
   # %1 should hold the username or try %-3
   VirtualDocumentRoot /home/%1/public_html
</VirtualHost>

Then you shouldn't have any problems with mod_dir.

You may have to uncomment the LoadModule (and AddModule for apache 1.3) lines for mod_vhost_alias.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

PreviousNext

Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 104 guests

cron