Mod_Rewrite Forums Forum Index Mod_Rewrite Forums
Provided by ModRewrite.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sub.domain.com/* -> domain.com/sub/* - rewrite
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    Mod_Rewrite Forums Forum Index -> Domain Handling
View previous topic :: View next topic  
Author Message
newbiewiz



Joined: 25 May 2006
Posts: 6

PostPosted: Tue May 30, 2006 10:54 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
richardk



Joined: 21 Dec 2005
Posts: 8798

PostPosted: Wed May 31, 2006 2:44 am    Post subject: Reply with quote

Just www.sub.domain:
Code:
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:
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.
Back to top
View user's profile Send private message
Uri



Joined: 31 May 2006
Posts: 3

PostPosted: Wed May 31, 2006 8:16 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
richardk



Joined: 21 Dec 2005
Posts: 8798

PostPosted: Wed May 31, 2006 8:30 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
thek-nar



Joined: 27 Jun 2006
Posts: 4

PostPosted: Tue Jun 27, 2006 6:50 pm    Post subject: Reply with quote

richardk wrote:
From: http://forum.modrewrite.com/viewtopic.php?p=4453#4453.
Code:
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!
Back to top
View user's profile Send private message
richardk



Joined: 21 Dec 2005
Posts: 8798

PostPosted: Wed Jun 28, 2006 3:03 am    Post subject: Reply with quote

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

Code:
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]
Back to top
View user's profile Send private message
thek-nar



Joined: 27 Jun 2006
Posts: 4

PostPosted: Wed Jun 28, 2006 12:03 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
richardk



Joined: 21 Dec 2005
Posts: 8798

PostPosted: Wed Jun 28, 2006 12:41 pm    Post subject: Reply with quote

Quote:
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:
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:
RewriteCond %1---%{REQUEST_URI} ^(.+)---//?~\1(/.*)?$

from the "mod_dir trailing slash fix" code.
Back to top
View user's profile Send private message
thek-nar



Joined: 27 Jun 2006
Posts: 4

PostPosted: Wed Jun 28, 2006 9:33 pm    Post subject: Reply with quote

richardk wrote:


Edit: or you could try removing this line
Code:
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)
Back to top
View user's profile Send private message
richardk



Joined: 21 Dec 2005
Posts: 8798

PostPosted: Thu Jun 29, 2006 2:18 am    Post subject: Reply with quote

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:
<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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Mod_Rewrite Forums Forum Index -> Domain Handling All times are GMT - 8 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group