1

I've been looking at htaccess and how to use rewrite rules in order to make a sort of "fake subdomain" for users.

so far i have:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.domain.com [R=301,NC,L]

RewriteCond %{HTTP_HOST} ^([a-z]+)\.domain\.com$
RewriteRule ^$ index.php?user=%1

this will take any /somthing and make it a subdomain and then pass the subdomain value in as a param to pick up.

i want to be able to now use actual parameters, and pass them through normaly with the extra "user" param from the "subdomain" e.g.

fred.domain.com/index.php?page=1&sort=up

would give me in $_GET

['user'] = 'fred'
['page']= 1
['sort'] = up

but for the life of me I cant figure out how to do this! as when i add any other params, I loose the user bit

Any help? =)

Also any helpful tutorials on htaccess would be nice! as all ones ive found haven't really explained what each bit does and why =\

Thanks in advance

OnIIcE
  • 811
  • 9
  • 27
  • You can use environment variables for such. See the apache documentation for mod_rewrite how to set them or reference them. Additionally you can do that inside your PHP script and the `$_SERVER` superglobal. – hakre Feb 11 '12 at 16:17
  • http://stackoverflow.com/questions/9732366/dynamic-subdomains-in-codeigniter-with-htaccess – santosh Sep 06 '12 at 04:02

3 Answers3

0

.htaccess file has nothing to do here. It can't help you.
To direct a user to your server, you have to alter DNS record, not web-server config.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Not quite. Most domains operated by hosting service provides offer a wild-card subdomain routing. So my DNS domain has a subdomain catch-all which points at my hosting service \(HS\) load-balance IP,94.136.40.100, as with all other shared hosting account holders. My HS decodes the HTTP_HOST to point all subdomain at my DOCRROT, and I have to use my DOCROOT `.htaccess` to process the subdomains. See http://blog.ellisons.org.uk/search-Webfusion for articles which give more explanation. – TerryE Feb 11 '12 at 17:19
  • I see nothing in your comment that contradicts with my answer, but thank you for your effort anyway. – Your Common Sense Feb 11 '12 at 17:42
  • If your HS provider has already set up a wild card DNS entry (as most do), then you've got _nothing_ to do in DNS and _all_ of the configuration is done in your DOCROOT `.htaccess` which kind of makes both of your statements at the least very misleading :( – TerryE Feb 11 '12 at 18:08
  • I still don't get the point. If you have your DNS setup already, it doesn't mean you don't need it at all. What's misleading here? – Your Common Sense Feb 11 '12 at 18:13
  • As most SHS providers set up a **\*** DNS A record for each account "you normally ***don't*** have to alter any DNS records." and first step in implementing what the OP wants is to configure his or her `.htaccess` file(s). So both of your points are pretty much 180% out, that's all. No big issue. Sorry. – TerryE Feb 11 '12 at 18:27
  • Okay, okay. You don't have to configure a DNS record if it's already configured. I am not arguing that, you are taking be wrong. There is nothing to configure in the .htaccess though. – Your Common Sense Feb 11 '12 at 18:43
0

I'm pretty sure you can set vhost *.yourdomain.com to point to a single path and then use mod_rewrite for the url parsing.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
NDBoost
  • 10,184
  • 6
  • 53
  • 73
0

I am assuming that:

  • You own a domain, domain.com say, that is being hosted by some shared hosting service (SHS) provider.
  • You either have access to a control panel to set up subdomains, or your SHS provider has already set up a * A record mapping to its shared service.
  • Some SHS providers simply map *.domain.com to a fixed subdirectory, say /webroot/domain.com/public_html; others do this top level-redirection for their users and provide a control panel which allow account-holders to associate subdomains with specific subdirectories.

So you need to work out which applies in your case:

  • by looking at your SHS providers FAQs,
  • by trying http://sss.domain.com/ to see if it routes to your DOCROOT
  • by using a phpinfo() script to see what Rewrite environment variable it uses to point to your DOCROOT. (For technical reasons in Apache, see Dynamic mass virtual hosts with mod_rewrite, %{DOCUMENT_ROOT} cannot be properly initialised for each user, so providers typically use a RewriteMap to initialise a stand-in; mine uses %{ENV:DOCUMENT_ROOT_REAL})

So let's assume that you want to set up a blog at say http://blog.domain.com/, you may need to:

  • Issue permanent redirects [R=301] from legacy references to http://domain.com/blog/* to http://blog.domain.com/*
  • Issue internal redirects from http://blog.domain.com/* to DOCROOT/blogdir/*
  • Add the necessary conditional interlocks to prevent infinite redirection loops, where this second internal redirect is then treated as a legacy reference.

I could give you a set of rewrite rules to do this, but given that I've answered a dozen flavours of this same Q this months, you can find lots of templates by searching or by looking at my blog (Webfusion, .htaccess) where I've written a number of articles giving more explanation.

If you want a single application to catch some wildcard subset of domain, as long as you can encode this in a regexp then you can do something like:

RewriteCond %{HTTP_HOST}    (sompattern)\.domain\.com
RewriteRule ^(?!appdir/).*  appdir/catchall.php?arg=$0&subdomain=%1 [L,QSA]

The (?!appdir/) bit is called a lookahead negative assertion and this stops the rule refiring on itself.

TerryE
  • 10,724
  • 5
  • 26
  • 48