subdomain to domain on the fly?

General Discussion of atomic repo and development projects.

Ask for help here with anything else not covered by other forums.
NightStorm
Forum User
Forum User
Posts: 60
Joined: Sun Dec 05, 2004 4:16 am

subdomain to domain on the fly?

Unread post by NightStorm »

I'm cross-posting this on several forums in hopes that someone somewhere will be able to help me with this. I've got a bit of a challenge. Running Plesk 7.5.3, and with a single domain I want to have sub.domain.com redirect automatically to domain.com/sub
I've set the wildcard entry in the domain's DNS, but the mod_rewrite part is kicking the crap out of me.
Basically, * must redirect, with the exception of webmail, chat, and www.

Here's what I have in the domains root vhost.conf so far (found it on the sw-soft forum), but it seems to create a loop that causes the browser to return an error about too many redirects.

Code: Select all

ServerAlias *.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain.com$
RewriteCond %{HTTP_HOST} !^chat\.domain.com$
RewriteCond %{HTTP_HOST} !^webmail\.domain.com$
RewriteCond %{HTTP_HOST} ^[^.]+\.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$1$2
Can anyone see what I'm missing in this?
breun
Long Time Forum Regular
Long Time Forum Regular
Posts: 2813
Joined: Sat Aug 20, 2005 9:30 am
Location: The Netherlands

Unread post by breun »

I have a domain running the following .htaccess:
Options +FollowSymLinks
RewriteEngine on

# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]
Post Reply