homepage Welcome to WebmasterWorld Guest from 54.205.122.62
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite subdomains
deimon



 
Msg#: 4049838 posted 8:59 am on Dec 27, 2009 (gmt 0)

Hello,
i have 1 domain for example .. my-domain.info
now i need automatic creation of subdomains by creating directory in root/sub/#*$!x .. i create by :

############################
RewriteEngine On
rewritebase /
php_flag register_globals on
Options +FollowSymLinks
Options -Indexes

RewriteCond %{HTTP_HOST} !^www\.my-domain\.info$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.my-domain\.info$ [NC]
RewriteRule ^(.*$) sub/%1/$1 [L]
############################

... sometime works, sometime no :( its true ?

i'd like if some directory (subdomain) isn't create .. so go 404.php .. now this write err500

---
my "dream" are :
automatic create subdomains with create directory

automatic-subdomain.my-domain.info/
=> my-domain.info/sub/automatic-subdomain/index.php

automatic-subdomain.my-domain.info/some
=> my-domain.info/sub/automatic-subdomain/some.php

automatic-subdomain.my-domain.info/some/somethink
=> my-domain.info/sub/automatic-subdomain/some.php?cat=somethink

automatic-subdomain.my-domain.info/some/somethink/somepage
=> my-domain.info/sub/automatic-subdomain/some.php?cat=somethink&page=somepage

many thanks.

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4049838 posted 6:03 pm on Dec 27, 2009 (gmt 0)

Two methods:

Rewrite only if the requested URL resolves to an existing directory and/or file in the /subs/ folder:

RewriteEngine On
RewriteBase /
php_flag register_globals on
Options +FollowSymLinks -Indexes
#
RewriteCond $1 !^sub/
RewriteCond %{HTTP_HOST} !^www\.my-domain\.info [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.my-domain\.info
RewriteCond %{DOCUMENT_ROOT}/sub/%1/$1% -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sub/%1/$1% -d
RewriteRule ^(.*)$ sub/%1/$1% [L]

-or-

Allow directory indexes of subdomain folders which do not have any index file:

RewriteEngine On
RewriteBase /
php_flag register_globals on
Options +FollowSymLinks [b]+I[/b]ndexes
#
RewriteCond $1 !^sub
RewriteCond %{HTTP_HOST} !^www\.my-domain\.info [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.my-domain\.info
RewriteRule ^(.*)$ sub/%1/$1 [L]

Test both of these so that you can see how the server behaviour changes when there are no files in the 'new' subdomain's folder. Then pick the method you prefer. The first is more like your code, while the second will have much better performance because it does not have to go check the disk (twice) for each request. It is also simpler.

After you get the subdomain->folder rewrite working, then you can modify the code to rewrite to a script like "some.php".

The RewriteCond at the top prevents an 'infinite' rewriting loop.

Note that there are several other changes to patterns, anchoring, and flags. These are intended to avoid problems -- some of which could have been serious.

Jim

deimon



 
Msg#: 4049838 posted 12:03 pm on Dec 29, 2009 (gmt 0)

many thanks.. i need change my requirements.
Now i change this htacces to.. its true ? and "secure" ?

###################################
RewriteEngine On
RewriteBase /
php_flag register_globals on
Options +FollowSymLinks +Indexes

RewriteCond $1 !^sub
RewriteCond %{HTTP_HOST} !^www\.my-domain\.info$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([[:alnum:]-]+)\.my-domain\.info$ [NC]
RewriteRule ^(.*$) sub/file.php?sub=%2 [L]
###################################

it makes ..
www.my-domain.info => www.my-domain.info/index.php

(www.)subdomain.my-domain.info => www.my-domain.info/sub/file.php?sub=subdomain

(www.)subdomain.my-domain.info/somethink/else/ => www.my-domain.info/sub/file.php?sub=subdomain

and by php > REQUEST_URI .. i will get "/somethink/else/", where i explode "/" and i get category, page etc. by position in url.

I want to using by 1 file - file.php, where i need know so i have this subdomains, and im in this location.

its possible or somewhere have better idea ?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4049838 posted 3:04 pm on Dec 29, 2009 (gmt 0)

You have a URL on both sides of your requirements list, above. Are you wanting an external redirect?

If this is supposed to be an internal rewrite, the right hand side will be an internal filepath not a URL.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved