homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

URL rewriting to point to subdomains
URL rewriting

 1:54 am on May 19, 2010 (gmt 0)


I need your help in rewriting the URL to match the pattern of my subdomain.

Here is the requirement.

I have the wildcard DNS entry pointing to my webserver IP and also created the virtual host under apache.

I have my webfiles under /var/www/html/wildcard

Every user has a exclusive folder under wildcard i,e /var/www/html/wildcard/user1 , /var/www/html/wildcard/user2, /var/www/html/wildcard/user3 etc.

The users would type out the URL as user1.mydomain.com and the webfiles should be served from /var/www/html/wildcard/user1 for the respectives users and the URL should remain as [user1.mydomain.com...]

I know these kind of solutions may already be there on this forum, i would appreciate if you could give a exact solution for my issue.

Thanks in Advance.




 2:48 am on May 19, 2010 (gmt 0)

Hi Sangishetty, and welcome to WebmasterWorld!

Please review our Apache Forum Charter [webmasterworld.com] for information on how to get the most from this forum.

To your specific question, one of these previous threads [google.com] will likely help you to get started and/or to focus you question more specifically (there are several hundred threads here on this subject alone, so it has been very well covered already).



 3:03 am on May 19, 2010 (gmt 0)

Thank You Jim,

I have hundreds of such posts and i am not able to hit at one specific to me , Hope i am clear with my requirement?

I want the user to see the URL as [user1.mydomain.com...] and the webfiles would be served from /var/www/html/wildcard/user1

Please point me to one speffic post which has the right solution which fits my requirement.

Sorry to bother you.



 3:05 am on May 19, 2010 (gmt 0)

currently without any rule when i type [user1.mydomain.com...] it is taking me directly to [user1.mydomain.com...] , I want to eliminate the trailing user1 i,e

I want the users to see only the URL [user1.mydomain.com...]


 4:00 am on May 19, 2010 (gmt 0)

You will likely never find a perfect plug-and-play solution. That is simply not the nature of server configuration code.

Review the threads found by the very-specific search I provided above, research the code you find in those threads, and adapt it to your exact needs -- many of those threads contain code that is already "extremely close" to what you need.

We here can help, but we cannot write code and debug it for everyone who asks -- There are simply not enough volunteers here, nor enough hours in the day, to support such an undertaking.

Therefore, we will endeavor to answer very-specific questions about *your code* and any difficulty you may have while testing it, in order to help you learn to support your own needs on your server.

Again, I'll ask you to to please review our Apache Forum Charter [webmasterworld.com] for information on the purpose of this forum, and how to get the most from it.



 2:14 am on May 20, 2010 (gmt 0)


I have the following rule in the htaccess.

I have restructured the folder and create a folder above the user folder called as sub

The current structure is /var/www/vhosts/wildcard/sub under which i have moved the user folders user1,user2,user3 etc.

rewriteEngine on
RewriteCond $1 !^sub/
rewriteCond %{HTTP_HOST} ^(www\.)?([a-z][a-z0-9_\-]{2,11})\.mydomain\.com [NC]
rewriteCond %{SCRIPT_FILENAME} !^/sub [NC]
rewriteRule ^(.*)$ /sub/%2 [C]

With the above rule in place it gets forwarded to the user directory but the URL is also reflecting the changes.

When i try [user1.mydomain.com...] it is changing the URL in browser as [user1.mydomain.com...]

I want the URL to remain as [user1.mydomain.com...]

Please help.



 2:18 am on May 20, 2010 (gmt 0)

And also if i have rule as follows.

rewriteEngine on
RewriteCond $1 !^sub/
rewriteCond %{HTTP_HOST} ^(www\.)?([a-z][a-z0-9_\-]{2,11})\.mydomain\.com [NC]
rewriteCond %{SCRIPT_FILENAME} !^/sub [NC]
rewriteRule ^(.*)$ /sub/$2 [C]

It is matching till /sub internally and not /sub/$2 and in the browser i see the URL to be as per my requirement [user1.myadomian.com....]

Is there a way i can match the user directory.



 2:55 am on May 20, 2010 (gmt 0)

I have also tried the following rule but noluck

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.mydomain\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.mydomain\.com(.*) /sub/$1

IT is changing browser url to www.user1.mydomain.com/sub/user1

Please help


 3:10 am on May 20, 2010 (gmt 0)

Back-reference the 'username' captured in the preceding RewriteCond pattern using %2

RewriteEngine on
RewriteCond $1 !^sub/
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9][a-z0-9\-]{1,10}[a-z0-9])\.example\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ /sub/%2/$1 [L]

Note adjustments to the "username" subdomain pattern to comply with host-naming requirements [tools.ietf.org] -- Underscores are not permitted in hostnames, and leading or trailing hyphens are not permitted.

The previous third rewritecond was removed, because it was redundant with the first rewritecond.

The new third rewritecond prevents a username of "www" to avoid conflicts with normal web usage of the www subdomain.


[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 2:10 am (utc) on May 21, 2010]


 3:22 am on May 20, 2010 (gmt 0)

Thank you Jim!

It works like a charm.



 2:06 am on May 21, 2010 (gmt 0)


I was try to browse a html page under one of the user directory /var/www/vhosts/wildcard/sub/user1 by URL

http://userr1.example.com/contactus.html but it still shows me index.html content instead of contactus.html content.

Any way out to browse individual pages under each user home directory?

Thank you in advance.


[edited by: jdMorgan at 2:12 am (utc) on May 21, 2010]
[edit reason] example.com [/edit]


 2:12 am on May 21, 2010 (gmt 0)

Yes, sorry Kranthi -- My error.

$2 in the final rule should be $1 instead. See the corrected code in my post above.



 2:16 am on May 21, 2010 (gmt 0)

Thank you Jim!

That worked , you are great!


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved