sub.domain.com/?page=x to www.domain.com/?site=sub&pag

Using a single web hosting account to host multiple sites

sub.domain.com/?page=x to www.domain.com/?site=sub&pag

Postby kapo » Sat Dec 02, 2006 10:19 pm

Hi all,

Firstly, thanks to everyone that contributes to this site (especially richardk). My site is now running under Plesk with wildcard subdomains!

My question now is what rewrite rule(s) could do something like this...

sub.domain.com/?page=xyz to www.domain.com/?site=sub&page=xyz

i.e The parameters passed are preserved and another parameter indicating the name of the sub domain is added

:-?

likewise, variations such as:
www.sub.domain.com/?page=xyz to www.domain.com/?site=sub&page=xyz

and:
www.sub.domain.com/index.php?page=xyz to www.domain.com/?site=sub&page=xyz


(btw, I have access to my httpd.conf, so not limited to .htaccess)

I am sure others would find this usefull too!

Thanks,
Kapo.
kapo
 
Posts: 6
Joined: Sat Dec 02, 2006 7:16 pm

Postby richardk » Sun Dec 03, 2006 3:03 pm

I assume all the sub domains have the same document root as domain.com?

In a .htaccess file in domain.com's document root:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /$1?site=%2 [QSA,L]


You could just extract the sub domain from the HTTP_HOST variable in the script.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby kapo » Mon Dec 04, 2006 1:12 pm

That works!! (once i removed the old rewrites from httpd.conf)

You are a legend! 8)



For everyone else, the only extra things to do are add a DNS entry for *.domain.com and add a line:

Code: Select all
ServerAlias *.domain.com


to your httpd.conf file ( or vhost.conf if you are using plesk like me )

(Dont forget to restart apache)


Note: This also works for multiple parameters so... sub.domain.com/?a=1&b=2&c=3 etc
will actually resolve to this... www.domain.com/?site=sub&a=1&b=2&c=3 etc
kapo
 
Posts: 6
Joined: Sat Dec 02, 2006 7:16 pm


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 85 guests

cron