0

I gave localhost an alias: 'myhost'. Entering 'myhost' in my address bar shows me /var/www/index.php. So far so good. Set up an apache virtual host on /var/www and called it 'myhost', then added three aliases: '*.myhost' '*.myhost.* 'myhost.*' the idea being that it would work like so:

myhost = site index/landing page/general stuff

user.myhost = general stuff specific to the user

myhost.topic = topical stuff not specific to a user

user.myhost.topic = topical stuff specific to a user

but all the data is loaded dynamically so the actual urls would be more like:

myhost/index.php?user=(empty or name of user)&host=myhost&app=(empty or topic)

I've spent two days trying to figure out the rewrite rules for this and no joy. Any help would be greatly appreciated.

EDIT: Also, do I have to add user1.myhost, user2.myhost, myhost.topic1, myhost.topic2, user1.myhost.topic1, user2.myhost.topic1, user1.myhost.topic2, user2.myhost.topic2, etc to my hosts file to make this work? If so, isn't there a better way?

bee.catt
  • 225
  • 1
  • 2
  • 7

1 Answers1

2

You can use these rewrite rules to redirect all domains to myhost with a host parameter.

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} myhost
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^(.*)?(.*)$ $1?$2&host=%{HTTP_HOST} [L]

RewriteRule ^(.*)$ http://myhost/$1?host=%{HTTP_HOST} [L]

You can also get domain name in your app (for example $_SERVER["SERVER_NAME"] in php) and use this value. So you dont need an extra host parameter for it.

You can install an easy-to-use dns server (like tinydns) and set wildcard subdomains like *.myhost etc. But user2.myhost.topic2 is not a valid domain. If your domain is "example.com", you subdomains should be "topic1.example.com" or "user1.topic1.example.com". You cant use "user1.example.topic1" as a domain in internet.

dirigeant
  • 221
  • 1
  • 4
  • I have a feeling this is very close, but it isn't working. I think it is looking for a subdomain folder, but I'm not using any. All content is served through a single file: /var/www/index.php. Basically, I want the url to reflect the change in content without refreshing the page (I'm using jquery for content fetching), or to load up the appropriate content (based on the query string) depending on what url is typed straight into the address bar. – bee.catt Aug 01 '12 at 15:41
  • So, for example: 'myhost' is '/index.php?user=&host=myhost&app=' while 'me.myhost.stuff' is '/index.php?user=me&host=myhost&app=stuff' – bee.catt Aug 01 '12 at 15:44
  • Sorry, i misunderstood your problem. So my first answer is irrelevant. – dirigeant Aug 01 '12 at 16:10
  • you can always edit it if you know a better solution. It's given me something to work with anyway, so if I don't get a better answer in the next few days, I'll accept your answer. Thank you for your effort. – bee.catt Aug 02 '12 at 01:47
  • 1
    I think, you can set wildcard subdomains (on dns) and wildcard virtual host (in web server) and send all requests to same "index.php" file. Then you can get domain/subdomain in your php app and get your data dynamically. You dont need any extra parameter for user/topic etc. and you dont need any rewrite to do this. – dirigeant Aug 02 '12 at 14:23
  • that's the route I'm working on now. I've installed bind9, and added a zone for myhost, with a wildcard subdomain CNAME. Now I'm trying to work out of what type the topical domains should be and the proper entries to get them to use the main domain as a subdomain. I'd thought running my own dns server would be too complicated, and thus opted for mod_rewrite, but it turns out to be a lot less confusing than trying to work out/interpret rewrite rules. – bee.catt Aug 02 '12 at 16:14
  • this particular part is just for personal use on my localhost as well, sort of like built in bookmarks that are easy to get to and remember, and easy to use in my code. I have a proper .ca domain name for the public side, but it wouldn't be serving the exact same content, or rather, it would be serving a small selection of what is on myhost, and probably formatted slightly differently as well. – bee.catt Aug 02 '12 at 16:20
  • Alright, now it's all working the way I want it to. I'm not sure it's all set up the "proper" way, but it works correctly. In bind, I created a master zone for myhost, with a wildcard subdomain CNAME (all user subdomains which do not exist in mysql user db will redirect to myhost via code); then I created a master zone for each topic pointing to the same ip as myhost, again with a wildcard subdomain as a CNAME for myhost with unused urls redirecting to a used address via code... – bee.catt Aug 02 '12 at 18:46
  • having a wildcard sub for the topical domain allows me to easily port app data to an external server in the future (for public information sharing) and access it locally in different ways, for example, I may want to use or manipulate the data in a general or public way (such as a wiki): point external.topic to topic.org; or use it for my own purposes either publicly: point publichost.topic to topic.my.ca; or privately: myhost.topic – bee.catt Aug 02 '12 at 18:57