Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [htaccess] Assigning users 'fake' subdomains

This thread is locked; no one can reply to it. rss feed Print
[htaccess] Assigning users 'fake' subdomains
ngiacomelli
Member #5,114
October 2004

I'd like to offer users who sign up to a website I'm developing a custom subdomain: username.domain.com. I'd like this address to point to basically return the contents of domain.com/view/username, so I figured htaccess was the way to go!

I use CodeIgniter as a framework, and have employed the following .htaccess to basically run everything through index.php:

RewriteEngine on
RewriteCond $1 !^(index\.phps)
RewriteRule ^(.*)$ /index.php/$1 [L]

Is there a simple way to redirect these subdomain requests? If so, is it a purely .htaccess solution, or will I have to tweak server settings? I have access to cPanel, if the latter is the case.

BAF
Member #2,981
December 2002
avatar

On playground, I setup mod_vhost_alias, which will dynamically map each subdomain to it's own folder.

In our situation, I set it up to listen separately on two IPs (one for CGames, one for me). For incoming requests that resolve to my IP, it maps to home/me/public_html/subdomain.domain (so if you go to http://blahblah.baf.cc/hi.html it would map to /home/me/public_html/blahblah.baf.cc/hi.html). That is a really simple setup.

On barfthedog, I have a more hierarchical setup like what you are talking about. Requests are mapped to public_html/domain/subdomain, so the same example from above would map to public_html/baf.cc/blahblah/hi.html.

I doubt you could even set up mod_vhost_alias without in depth access to the server configuration.

ngiacomelli
Member #5,114
October 2004

Quote:

I doubt you could even set up mod_vhost_alias without in depth access to the server configuration

This isn't a problem, as I can contact my host and ask that this be configured. I was just wondering exactly how to configure it, so that I can pass that information along, as well.

CGamesPlay
Member #2,559
July 2002
avatar

If all of your subdomains already go to the same documentroot, just use .htaccess.

RewriteCond %{HTTP_HOST} !example.com
RewriteRule (.*) /index.php/user/customPage/%{HTTP_HOST}/$1 [L]

That will send a request for http://cgamesplay.example.com/ to /index.php/user/customPage/cgamesplay.example.com/, and http://cgamesplay.example.com/profile/music to /index.php/user/customRequest/cgamesplay.example.com/profile/music.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Neil Walker
Member #210
April 2000
avatar

Perhaps you want to use NC to cater for them typing names in a different case.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

CGamesPlay
Member #2,559
July 2002
avatar

There are no case-sensitive rules in any of the expressions provided here. NC wouldn't have any effect :P

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Neil Walker
Member #210
April 2000
avatar

It was more to fix somebody typing the wrong case url but fixing it for them.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

CGamesPlay
Member #2,559
July 2002
avatar

In that case, using the NC flag wouldn't "fix" the URL, it would just allow both to work. Good SEO practices would dictate that, if you do receive a request for the wrong case and can still process it (for example in the case of the user name), send the client a 301 Moved Permanently redirect.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ngiacomelli
Member #5,114
October 2004

I got everything working quite nicely, but have hit another interesting problem. This time with some Javascript (I use the Mootools library). Basically, I have a function which generates an Ajax request:

function fetch_latest_entries() {
 
   new Ajax('fetch', {
  method: 'get',
        onComplete: function() {
 
      // yadda yadda
 
        }
    }).request();
 
}

This works great when I call it from domain.com/account. However, I assign each user on my site their own subdomain, like so: username.domain.com. Within cPanel I've setup *.domain.com to point towards domain.com/user/view/{username}.

When I execute the above function from username.domain.com, Firebug returns the following:

Quote:

[Exception... "'Permission denied to call method XMLHttpRequest.open' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "" data: no]

A little snooping around on Google has led me to believe that this is because the XMLHttpRequest considers a subdomain and a domain to be different locations. How can I solve this?

CGamesPlay
Member #2,559
July 2002
avatar

Put the request on the same subdomain.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ngiacomelli
Member #5,114
October 2004

Quote:

Put the request on the same subdomain.

I'm not sure I follow? The subdomain (username.domain.com) is a fake that points to domain.com. So there is no physical subdomain.

CGamesPlay
Member #2,559
July 2002
avatar

Great, since it's the same server, you don't have to do any work to make http://subdomain.domain.com/ajax/request work. You have to have the request be on the same domain as the security context, so, move the request.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ngiacomelli
Member #5,114
October 2004

Sorry for being really dense, but move the query where, exactly?

CGamesPlay
Member #2,559
July 2002
avatar

The AJAX request should open http://subdomain.domain.com/ajax/request, and it will then be on the same subdomain.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ngiacomelli
Member #5,114
October 2004

I made the Ajax request url username.domain.com/fetch/personal, but sadly this still doesn't work. I'm using the following .htaccess file:

RewriteEngine on

# Prevent infinite loop
rewriteCond $1 !^index.php/user/show/

RewriteCond %1 !^www$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com 
RewriteCond %1 !^www$ [NC]
RewriteRule (.*) index.php/user/show/%1/$1 [L]

RewriteCond $1 !^(index\.php|images|robots\.txt|css|js|img|uploads|avatars)
RewriteRule ^(.*)$ /index.php/$1 [L]

(Note: I've changed the actual domain to domain.com to post here)

This basically forces: username.domain.com to point towards index.php/user/show. If I were to say go to: username.domain.com/js/fetch.js - I'm passed to index.php. I'm sure the problem stems from this. Any suggestions for an alternate .htaccess?

Go to: