Link to home
Start Free TrialLog in
Avatar of Spunkymungbeans
Spunkymungbeans

asked on

mod_rewrite for subdomains

I would like to achieve the following with mod_rewrite:

http://www.shire-biz.com - No change
or
http://shire-biz.com - No change


http://foo.shire-biz.com
to
index.php?option=com_webpage&username=foo

and

http://foo.shire-biz.com/page1.htm
to
index.php?option=com_webpage&username=foo&page=1


This has been suggested but isn't working:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?shire-biz\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.])\.shire-biz\.com [NC]
RewriteRule ^(page([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2 [L]

Max points for urgency.  Any help is appreciated.
Avatar of ramazanyich
ramazanyich
Flag of Belgium image

Try following config:

RewriteCond %{HTTP_HOST} foo.shire-biz.com [NC]
RewriteCond %{REQUEST_URI} ^page.+
RewriteRule ^page(\d+)\.htm index.php?option=com_webpage&username=foo&page=$1 [L]
RewriteCond %{HTTP_HOST} foo.shire-biz.com [NC]
RewriteRule .+ index.php?option=com_webpage&username=foo [L]

Avatar of Spunkymungbeans
Spunkymungbeans

ASKER

'foo'  is a variable which could be anything. The subdomain represented by 'foo' will never be set up as a virtual host, which is why I need the mod_rewrite.
there's a + missing

([^.]+)


RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?shire-biz\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.shire-biz\.com [NC]
RewriteRule ^(page([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2 [L]
btw. the rewrite rule will only work in per-dir context; if you use it in per-server context (virtualhost, main server config section), you'll need to add a leading slash to the pattern:

RewriteRule ^/(page([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2 [L]
Sorry its still not working.  Here is the entry from my httpd.conf file:

<VirtualHost *>
    ServerName shire-biz.com
    ServerAlias www.shire-biz.com
    DocumentRoot /var/www/html/shirebiz
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^(www\.)?shire-biz\.com [NC]
  RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.shire-biz\.com [NC]
  RewriteRule ^/(page([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2 [L]
</VirtualHost>

You can test it by going to http://www.shire-biz.com.  If you type http://smbeans.shire-biz.com you will be taken to another site - the main site on the server.   If it works you will remain at shire-biz but be shown a testpage.
ASKER CERTIFIED SOLUTION
Avatar of caterham_www
caterham_www
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes!  That did it.

Here is the full solution:

<VirtualHost *>
    ServerName shire-biz.com
    ServerAlias www.shire-biz.com *.shire-biz.com
    DocumentRoot /var/www/html/shirebiz
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^(www\.)?shire-biz\.com [NC]
  RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.shire-biz\.com [NC]
  RewriteRule ^/(page([0-9]+)\.htm)?$ /index.php?option=com_webpage&username=%2&page=$2 [L]
</VirtualHost>