Rewriting URL's

Using a single web hosting account to host multiple sites

Rewriting URL's

Postby jusstakas » Wed Feb 18, 2009 2:20 am

Hi,
I have enabled Apache2 mod userdir and all users can reach their websites using url for ex.: mydomain.com/~user

I need to rewrite this url as user.mydomain.com and mydomain.com/user

How can i do that with mod rewrite ?

thanks for the answers
jusstakas
 
Posts: 5
Joined: Wed Feb 18, 2009 1:50 am

Postby richardk » Wed Feb 18, 2009 10:43 am

Where are the user directories?

I think mod_vhost_alias would be the best thing to use
Code: Select all
<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com

  DocumentRoot /main/document/root
</VirtualHost>

<VirtualHost *:80>
  ServerName a.example.com
  ServerAlias *.example.com

  VirtualDocumentRoot /users/%-3/public_html
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jusstakas » Thu Feb 19, 2009 7:21 am

Hey,
thanks for your reply. But i still have a problem and i'm searching for the solution. well i'll try to describe it in more detailed way.

At first i did not know about user_dir so i created a vrtual host file for each user in /etc/apache2/sites-available and messed up my web server with such a lot of useless files.

My users are located at /home/username/public_html

for ex. /home/1st_user/public_html
/home/2nd_user/public_html
and etc.

So when i found out about user_dir mod i realised that i can have one single configuration to all my users.
here's my userdir conf:

Code: Select all

<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root

        <Directory /home/*/public_html>
                AllowOverride All
              #  FileInfo AuthConfig Limit
                Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
        </Directory>

<Directory /home/*/public_html/cgi-bin/>
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
    AddHandler cgi-script .cgi .pl
  </Directory>

</IfModule>



So i need understand where to put my mod rewrite rules and what are the ruls that i should put to.

And where actually put the code that u've shown to me.

in my opinion i just need to put a tricky rewrite rule code that server understand www.username.example.com as www.example.com/~username
May be you know a beter way to automate it or how to do in such kind a way?

by the way will mod_vhost_alias (that u showed to me) automate that as mod rewrite in one single configuration?

i know that creating a new virtual host file for each user by hand is not a solution


this problem i'm trying to solve for about a month and still not success.

waiting for the answers, thank u thank u...
jusstakas
 
Posts: 5
Joined: Wed Feb 18, 2009 1:50 am

Postby richardk » Sat Feb 21, 2009 1:23 pm

At first i did not know about user_dir so i created a vrtual host file for each user in /etc/apache2/sites-available and messed up my web server with such a lot of useless files.

My users are located at /home/username/public_html

for ex. /home/1st_user/public_html
/home/2nd_user/public_html
and etc.

That's why you should use mod_vhost_alias and VirtualDocumentRoot. Read the documentation.

in my opinion i just need to put a tricky rewrite rule code that server understand www.username.example.com as www.example.com/~username

Mod_rewrite is the wrong way to do this.

by the way will mod_vhost_alias (that u showed to me) automate that as mod rewrite in one single configuration?

Yes. You have one <VirtualHost> that works for all sub domains.

Code: Select all
<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com

  DocumentRoot /main/document/root
</VirtualHost>

# For all user sub domains.
<VirtualHost *:80>
  ServerName a.example.com
  ServerAlias *.example.com

  VirtualDocumentRoot /home/%-3/public_html
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jusstakas » Sun Feb 22, 2009 10:05 am

Hey, thanx

everything works fine

but could u tell me, what should i write to make it work example.com/user as well as user.example.com

ServerAlias *.example.com example.com/*

is this right?

example.com/user still not working

thanx
jusstakas
 
Posts: 5
Joined: Wed Feb 18, 2009 1:50 am

Postby richardk » Sun Feb 22, 2009 12:38 pm

Try
Code: Select all
<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com

  DocumentRoot /main/document/root

  Options +FollowSymLinks

  RewriteEngine On

  RewriteCond /home/$1/public_html/ -d
  RewriteRule ^([^/]+)(/(.*))?$ /home/$1/public_html/$3 [PT,L]
</VirtualHost>

# For all user sub domains.
<VirtualHost *:80>
  ServerName a.example.com
  ServerAlias *.example.com

  VirtualDocumentRoot /home/%-3/public_html
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jusstakas » Tue Feb 24, 2009 3:24 pm

Hey,
thank you, you helped me so much
still some bugs
i put that code in virtual host file, but no changes, so i created .htaccess file and put it in the root directory of www.example.com
Code: Select all

<IfModule mod_rewrite.c>
RewriteEngine On

Options +FollowSymLinks
RewriteCond /home/$1/public_html/ -d
RewriteRule ^([^/]+)(/(.*))?$ /home/$1/public_html/$3 [PT,L]

</IfModule>



and when i typed www.example.com/someuser i got 500 internal server error

and when i typed www.example.com/notexist i got 404 page not found and thats ok becouse this user does not exist but the reaction for the existing user was 500 server error, that showed to me that mod rewrite is trying to do smth when existing user is found, but its not working
P.S mode rewrite is enabled and it is working properly with other rules
(when i tried the code u showed to me i disabled other my own created rules to assure that my own rewrited rules are not causing an error in reaction with yours)
jusstakas
 
Posts: 5
Joined: Wed Feb 18, 2009 1:50 am

Postby richardk » Tue Feb 24, 2009 3:59 pm

Could /user redircet to user.example.com instead?

and when i typed www.example.com/someuser i got 500 internal server error

What does your error log say is causing the error?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jusstakas » Tue Feb 24, 2009 5:20 pm

Yes, /user can redirect to user.example.com with mod_vhost_alias configuration that you posted in previous post

but when i try to go to /user error log say:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

hope this helps to understand the problem

thanx
jusstakas
 
Posts: 5
Joined: Wed Feb 18, 2009 1:50 am

Postby richardk » Wed Feb 25, 2009 8:19 am

Yes, /user can redirect to user.example.com with mod_vhost_alias configuration that you posted in previous post

Try
Code: Select all
Options +FollowSymLinks

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond /home/$1/public_html/ -d
  RewriteRule ^([^/]+)(/(.*))?$ http://$1.example.com/$3 [R=301,L]
</IfModule>


but when i try to go to /user error log say:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

In a .htaccess file it would not work anyway, because you cannot rewrite to a location outside of the current document root.

The error says it's looping but that should only happen if /home/home/public_html/ exists (or you have other rules).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 19 guests

cron