Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mod rewrite for subdomains

mod rewrite for subdomains



2:28 am on Jan 19, 2006 (gmt 0)

5+ Year Member

Hello to all.

My english are not so good, i'm from Spain (Canary Islands).

I'm trying since two weeks to use the rewrite mod of Apache to use it to "create" subdomains.

I search at google, and in forums (spanish forums), but though there are examples, a lot of, and here at wemasterworld more, i can't bring it to run. Allways errors, or no errors, but not work.

I am driven to despair since I go some time trying to do that this works, but there is no way.

I know that in this forums are a lot of examples, but, until now, i've prove five or six, and no one have worked.

I think it's not a good idea post one of the thousend (not so much :P) of codes i've seen and trying, because i unknow where is the most correctly.



3:14 am on Jan 19, 2006 (gmt 0)

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


Welcome to WebmasterWorld!

We have discussed many ways of doing this here at WebmasterWorld. Perhaps this search [google.com] will help you.

For background information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].



12:48 pm on Jan 20, 2006 (gmt 0)

5+ Year Member

Hello another one.

I've tried some codes from webmasterworld, but anything works. The "best" (without errors when i try), is that one, from andreas.

RewriteEngine On
rewritecond %{http_host}_SPACE_!^$
rewritecond %{http_host}_SPACE_!^(www\.)?anydomain\.com$ [NC]
rewritecond %{http_host}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+)? [NC]
rewritecond %2<->%3_SPACE_!^(.*)<->\1$ [NC]
rewriterule ^(.+) /%2/$1 [PT,L]

Give no errors, but doesn't works. When i try to open a subdomain, there give me a 404 error.

I've put spaces before!, not _SPACE_.


5:39 pm on Jan 20, 2006 (gmt 0)

5+ Year Member

I remenber, a time ago... That i've change some things in httpd.conf, but, PLESK (i use plesk), change me my configuration to plesk's configuration, the i must use htaccess to configure these things.

It's possible, that the code above don't work because i use PLESK?


10:46 pm on Jan 20, 2006 (gmt 0)

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

I don't know about Plesk, but many control panels create a separate subdirectory for each subdomain, and then add code to httpd.conf to 'send' user requests for that subdomain directly to that suddirectory. Therefore, no centralized .htaccess file can handle all subdomains.



7:33 pm on Jan 21, 2006 (gmt 0)

5+ Year Member

The backref \1 in this part doesn't work in the newer version fo Apache1.3, AFAIK.
rewritecond %2<->%3_SPACE_!^(.*)<->\1$ [NC]

I think it worked up to Apache 1.3.27 or so.

I wrote several different version of sub (and pointed) domain RewrireRules.

Here is one of them.

RewriteEngine on
RewriteBase /

# Loop stopping and Virtual document root processing for CGI/PHP
#RewriteRule ^/*virtualdr.cgi - [L]
#RewriteRule ^/*[^./]+\.[^/]+/.*\.(cgi¦php5?)$ virtualdr.cgi [L]
RewriteRule ^/*[^./]+\.[^/]+/.*$ - [L]

# Code for missing Trailing slashe problem
RewriteCond s%{HTTPS} ^((s)on¦s.*)$
RewriteRule ^/*(.+/)?([^.]*[^/])$ http%2://%{HTTP_HOST}/$1$2/ [L,R=301]

# Sub/pointer domain rewrite code
RewriteCond %{HTTP_HOST}!^(www\.)?(MAINDOMAIN\.?COM¦USER) [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ %2/$1

Directory layout:
/path-to-docroot/ <== MainDomain in docroot
/path-to-docroot/subdom1.maindomain.com/ <== Directory for sub domain
/path-to-docroot/subdom2.maindomain.com/ <== Directory for sub domain
/path-to-docroot/pointed-domain.com/ <== Directory for pointed domain
/path-to-docroot/sub.pointed-domain.com/ <== Directory for sub domain of pointed domain

The virtualdr.cgi prevents the problem of ugly URL and
some scripts simply failing when the docroot is presented differently.

I'll post URL for this and other code and explanation.
if it's allowed
If not, you can google and it appears near the top with "virtualdr.cgi".


Featured Threads

Hot Threads This Week

Hot Threads This Month