rewrite subdomain and url

Using a single web hosting account to host multiple sites

rewrite subdomain and url

Postby dudedudedude » Tue Feb 24, 2009 7:34 pm

So here's what up. I am trying do several things here with mod rewrite. The part I am getting hung up on is grabbing both the sub domain and everything to the right of the tld.

I can get these two features to work independently but cant seem to combine them.

Can anyone point me in the right direction?


Code: Select all
Options +FollowSymLinks
Options +Indexes
RewriteEngine On

# If no-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^sample\.com
RewriteRule (.*) http://www.sample.com/$1 [R=301,L]
#
# If www+subdomain or subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.sample\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.sample\.com
RewriteRule (.*) http://%1.sample.com/$1 [R=301,L]
#
#
# BELOW IS WHERE IM RUNNING INTO TROUBLE
#
# Takes subdomain and adds it to the end of the url as $_GET['acct']
RewriteCond %{HTTP_HOST} !^www\.sample\.com
RewriteCond %{HTTP_HOST} !^blog\.sample\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.sample\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?acct=%1 [L]
#
#
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) index.php?page=$1 [QSA,L]
[/code]
dudedudedude
 
Posts: 3
Joined: Tue Feb 24, 2009 7:15 pm

Postby dudedudedude » Tue Feb 24, 2009 8:40 pm

The goal is to take

http://user.sample.com/testpage/

and produce

index.php?acct=user&page=testpage
dudedudedude
 
Posts: 3
Joined: Tue Feb 24, 2009 7:15 pm

Postby richardk » Wed Feb 25, 2009 8:28 am

Do both parts in the same rule
Code: Select all
Options +FollowSymLinks +Indexes

RewriteEngine On

# If no-www domain requested, externally redirect to www domain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) http://www.sample.com/$1 [R=301,L]

# If www+subdomain or subdomain+www domain requested, externally redirect to subdomain without "www".
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://%1.sample.com/$1 [R=301,L]

# user.sample.com/testpage/ --> /index.php?acct=user&page=testpage
RewriteCond %{HTTP_HOST} !^(blog|www)\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?acct=%1&page=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby dudedudedude » Wed Feb 25, 2009 4:27 pm

Thanks so much. That works super well.

There is one problem. When there is no value for 'page' i dont get a value for the sub domain. any suggestions for how to handle that?

e.g.

sub.sample.com

i would like to receive

index.php?acct=sub

or even

index.php?acct=sub&page=


Thanks again! youve really been helpful.
dudedudedude
 
Posts: 3
Joined: Tue Feb 24, 2009 7:15 pm

Postby richardk » Thu Feb 26, 2009 6:58 am

Before
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-d

add
Code: Select all
RewriteCond %{REQUEST_URI} ^/$ [OR]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 21 guests

cron