Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

virtual hosting with .htaccess file

virtual hosting with .htaccess on apache 1.3


Star Crescent

7:35 am on Feb 2, 2009 (gmt 0)

5+ Year Member

I am trying to do this:

I have tree domain names
1. vvvvvv.org
2. yyyyyy.com
3. zzzzzz.com

Only the vvvvvv.org has the original IP adress the rest are routed to vvvvvv.org later. I want to redirect the yyyyyy.com and zzzzzz.com addresses to their respective directories under vvvvvv.org which are vvvvvv.org/yyydir and vvvvvv.org/zzzdir.
I am on a rented webhosting and do not have access to httpd.conf. I have to do it with .htaccess
The host is running Apache 1.3.41

I want www.vvvvvv.org appear as itself no matter what you enter vvvvvv.org or www.vvvvvv.org
I want www.vvvvvv.org/yyydir appear as www.yyyyyy.com no metter what you enter yyyyyy.com or www.yyyyyy.org
I wanr www.vvvvvv.org/zzzdir appear as www.zzzzzz.com no metter what you enter zzzzzz.com or www.zzzzzz.org

By no means I am no expert on either programming nor scripting like this. However, so far I have read a lot and came up with the following:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.yyyyyy\.com
RewriteCond %{REQUEST_URI} !^/yyyyyy/
RewriteRule ^(.*)$ [vvvvvv.org...] [R=301,L]

RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteCond %{HTTP_HOST} !^www\.vvvvvv\.org
RewriteCond %{HTTP_HOST} ^www\.yyyyyy\.com
RewriteRule ^(.*)$ [vvvvvv.org...] [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.zzzzzz\.com
RewriteCond %{REQUEST_URI} !^/zzzzzz/
RewriteRule ^(.*)$ [vvvvvv.org...] [R=301,L]

RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteCond %{HTTP_HOST} !^www\.vvvvvv\.org
RewriteCond %{HTTP_HOST} ^www\.zzzzzz\.com
RewriteRule ^(.*)$ [vvvvvv.org...] [R=301,L]

RewriteBase /
RewriteCond %{REQUEST_URI} !^/robots\.txt$ [NC]
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ [%1...] [R=301,L]

Sadly, it's not doing what I am intended. When I write yyyyyy.com it goes to www.vvvvvv.org. When I write www.yyyyyy.com it goes to www.vvvvvv.org/yyydir as intended but the adress bar of the browser shows it as www.vvvvvv.org/yyydir not www.yyyyyy.com

How can I fix it?


3:31 pm on Feb 2, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Use the "internal rewrite" syntax of mod_rewrite, not the "external redirect" syntax. By definition, an external redirect "tells" the client (browser or robot) to ask for the content again at a new URL, so the client browser will update it's address bar, and search engines will update the URL in their index database.

Also, you should place you external redirects first, in order from most-specific pattern (least URLs affected) to least-specific pattern, followed by internal rewrites, again in order from most-specific to least-specific pattern.

Do not accept mixed-case hostnames or hostnames with an appended FQDN indicator (a trailing period) or port number; Redirect them to the canonical domain.

It is not clear what some of the "special conditions" in your code are for, so I can't quite figure out exactly what you intended to do. Code comments are something that I highly recommend, both to make your code clear when posted in forums, and to remind you of what the code is intended to do when you look at it again in six months (or after six years).

In order to simplify the code and make future growth easier, it's often a good idea to put the alternate domains into a separate "hosted sites" subdirectory or to "tag" the filepath in some other obvious way. This keeps the top-level directory simple and tidy, but more importantly, allows you to recognize a previously-rewritten path so that you don't rewrite it again... avoiding recursion. In the code below, I show code that assumes that files for "www.foo.com" are stored in the subdirectory "/vh_www.foo.com/". The rewriterule looks at the current URL-path and won't rewrite it if it already starts with "vh_". This easily avoids the recursion problem that you would have otherwise. You can use any subdirectory name or prefix you like, I just picked "vh_" for "virtual hosts."

Taking all that into account, I'd use:

Options +FollowSymlinks
RewriteEngine on
# Externally redirect non-canonical vvvvvv.org domain requests to canonical domain
RewriteCond %{HTTP_HOST} ^([^.]+\.)*vvvvvv\. [NC]
RewriteCond %{HTTP_HOST} !^www\.vvvvvv\.org$
# temporary exception for access using IP address
RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteRule (.*) http://www.vvvvvv.org/$1 [R=301,L]
# Externally redirect non-canonical yyyyyy.com domain requests to canonical domain
RewriteCond %{HTTP_HOST} ^([^.]+\.)*yyyyyy\. [NC]
RewriteCond %{HTTP_HOST} !^www\.yyyyyy\.com$
RewriteRule (.*) http://www.yyyyyy.com/$1 [R=301,L]
# Externally redirect non-canonical zzzzzz.com domain requests to canonical domain
RewriteCond %{HTTP_HOST} ^([^.]+\.)*zzzzzz\. [NC]
RewriteCond %{HTTP_HOST} !^www\.zzzzzz\.com$
RewriteRule (.*) http://www.zzzzzz.com/$1 [R=301,L]
# Rewrite requests for <anything>.<domain>.<tld> to /vh_<domain>.<tld> subdirectory
# except for vvvvvv.org or its IP address
RewriteCond %{HTTP_HOST} !^www\.vvvvvv\.org
RewriteCond %{HTTP_HOST} !^001\.001\.001\.001
RewriteCond $1 !^vh_
RewriteCond %{HTTP_HOST} ^(([^.]+\.)*([a-z\-]+\.[a-z]{2,6}))$
RewriteRule (.*) /vh_%1/$1 [L]

This code may not do exactly what you want to do; However, I hope it's useful as an example.

Files for www.vvvvvv.org are stored in /
Files for www.yyyyyy.com are stored in /vh_www.yyyyyy.com/
Files for www.zzzzzz.com are stored in /vh_www.zzzzzz.com/

Any request with a non-canonical hostname will be redirected to the canonical domain. For example, the following requested hostnames will be redirected:

vvvvvv.org (no www) --> www.vvvvvv.org
www.vvVvvv.org (uppercase) --> www.vvvvvv.org
www.vvvvvv.org. (FQDN) --> www.vvvvvv.org
www.vvvvvv.org:80 (appended port number) --> www.vvvvvv.org
www.vvvvvv.org.:80 (FQDN and port) --> www.vvvvvv.org

You may also want to add exceptions to the domain-to-subdirectory rewrite rule; I removed your exception for the robots.txt file because each domain should have its own robots.txt file, but you may want to add exceptions for shared image or script directory requests, etc. which should not be rewritten to the domain-subdirectory.


Star Crescent

7:55 pm on Feb 2, 2009 (gmt 0)

5+ Year Member

This code is exactly do what I hope it to do.

This apache syntax reminds me C, which sounds (!) to me like "gibberish". You are very kind that you included all the comments. A real coder's work.

Sadly, it show how poorly I understood this stuff. Maybe I am in the wrong business.

Many thanks. It's great help to me.


3:20 am on Feb 3, 2009 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Well, mod_rewrite itself *was* written in 'C'

Both make use of regular-expressions pattern-matching as well.

Glad it worked for you, Ay Yildiz :)


[edited by: jdMorgan at 3:31 am (utc) on Feb. 3, 2009]


3:34 pm on Feb 3, 2009 (gmt 0)

5+ Year Member

Post deleted; See [webmasterworld.com...]

[edited by: jdMorgan at 4:09 pm (utc) on Feb. 3, 2009]
[edit reason] Duplicate post [/edit]


Featured Threads

Hot Threads This Week

Hot Threads This Month