Forum Moderators: phranque

Message Too Old, No Replies

subpages in subdomain

how?

         

omoutop

8:14 am on Mar 30, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, purpose is to have unlimited number of subdomains, created dynamically for our members.
So, a request to www.example.com/sub_index.php?user=demo will translate to demo.example.com url.

I have enabled name server and web server to accept wildcard requests (actually i contact my hosting server to do that).

On the htaccess part i have the following:

#rewrite example.com into www.example.com
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.htm([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]

#subdomains
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^(.*)$ sub_index.php?user=%1

So, a request to demo.example.com/ and to demo.example.com/index.php is served by the www.example.com/sub_index.php?user=demo

My problem is that i cant find a way to have subpages work in the subdomain
For example: demo.example.com/somepage.php should be served by www.example.com/sub_page.php?user=demo

Any help appreciated.
Thanks

jdMorgan

11:49 pm on Apr 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Internally rewrite all index-page requests for all non-www subdomains to the
# sub_index.php script, passing the requested subdomain as "user=" parameter
# prevent recursion
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^$ /sub_index.php?user=%1 [L]
#
# Internally rewrite all other .php page requests for all non-www subdomains
# to the requested page, passing subdomain as "user=" parameter
# prevent recursion
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^(.+\.php)$ /$1?user=%1

Note that as coded, any query strings appended to client-requested URLs will be replaced by the "user=xyz" query string. If you need to preserve client-requested query strings, use the [QSA,L] flag on the rewriterules.

Requests for URLs specifying anything other than index pages or php scripts were not described in the requirements, and so are not handled in this code. Therefore, you'll have to decide what to do with requests for image, css, JavaScript, and media files if they will require any special handling.

Jim

omoutop

6:00 am on Apr 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for this Jim.
If i understand this correctly, the [QSA,L] flag will act somewhat like this:

some_page.php?user=demo&id=5 will be converted to demo.example.com/page.php?id=5

I will go read on this

omoutop

11:49 am on Apr 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Back again with more questions on this.
Following your advise I have setup 2 sets of rules

1 for the index pages

RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^$ /sub_index.php?user=%1 [L]


And 1 for all other pages. Going one step further, i decided to have one rule for all sub pages inside my subdomains.
I thought of using something like this:
demo.example.com/some_page.php?id=1 will be served by sub_some_page.php?user=demo&id=1
demo.example.com/other_page.php will be served by sub_other_page.php?user=demo

So, i constructed the following rule.

RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^/([A-Za-z0-9_]+\.php)$ /sub_$1?user=%1 [QSA,L]


Purpose was to have only 1 rule for all my sub pages.
Unfortunately, this approach failed me.
Index request works
Sub page request gives me a 404 error (http://demo.example.com/other_page.php?id=5&id2=6), although the sub_other_page.php is uploaded to the root folder.

What do i miss?

omoutop

7:47 am on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



ok i got one step further and i am really close to complete this.


#######################################################
##### rewrite example.com into www.example.com #####
######################################################
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]




###########################
## subdomains
###########################

#index
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^index\.php$ /sub_index.php?user=%1 [L]


#other pages
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.roomres.info
RewriteRule ^([^\.]*)\.php$ /sub_$1.php?user=%1 [QSA,L,R]


A request to www.example.com is served by www.example.com/index.php.
A request to demo.example.com is served by www.example.com/sub_index.php?user=demo
A request to demo.example.com/other_page.php?id=5 should be served by example.com/sub_other_page.php?user=demo&id=5, but fails.
Instead i get demo.example.com/sub_other_page.php?user=demo&id=5

I assume something forces two rewrite rules to apply.
Since i have no access to mod_rewrite logs, can someone advice on what i miss?

thanks

jdMorgan

3:32 pm on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that you have an "R" flag on your "#other pages" rule, which makes it an external 302 redirect... Not recommended, nor likely intended.

Jim

omoutop

6:22 am on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks Jim, that did it
so, final code for subdomains and pages in subdomains (for whoever needs it) is:


#######################################################
##### rewrite example.com into www.example.com #####
######################################################
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

###########################
## subdomains
###########################
RewriteCond %{QUERY_STRING} !^([^&]*&)*user=[^&]+
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com
RewriteRule ^([^\.]*)\.php$ /sub_$1.php?user=%1 [QSA,L]


This code will take any page named: sub_[PAGE].php?user=demo&id=5
and create demo.example.com/[PAGE].php?id=5