Welcome Guest, Not a member yet? Register   Sign In
Giving users subdomains
#1

[eluser]Nial[/eluser]
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! Via cpanel I've configured *.domain.com to point to domain.com, and us the following .htaccess:

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

Is there a simple way to redirect these subdomain requests to convert user.domain.com -> domain.com/view/user via .htaccess while maintaining the above rule?
#2

[eluser]Pascal Kriete[/eluser]
Try something like this:
Code:
RewriteEngine on

# Prevent infinite loop
rewriteCond $1 !^view/

# Rewrite to subdomain
rewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
rewriteRule (.*) /view/%1/$1 [L]

# Add index.php
RewriteCond $1 !^(index\.phps)
RewriteRule ^(.*)$ /index.php/$1 [L]
#3

[eluser]Nial[/eluser]
Thanks for the fast reply. Sadly, that results in a 500 Internal Server Error. Basically, I'm looking for:

user.domain.com to point towards a CI controller (domain.com/index.php/controllername - or, as I have it configured currently domain/controllername). In this case, user.domain.com -> domain.com/view/user
#4

[eluser]Pascal Kriete[/eluser]
Right, that's what I was shooting for. Try the code without the index.php part and change the RewriteRule to /index.php/view/....
#5

[eluser]Nial[/eluser]
Using the following (having changed the domain back to domain.co.uk to post here):

Code:
RewriteEngine on

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

# Rewrite to subdomain
rewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.co.uk
rewriteRule (.*) index.php/view/%1/$1 [L]

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

I get the CI 404 Page Not Found error, which is progress. the view controller does exist, however.
#6

[eluser]Pascal Kriete[/eluser]
Ah yes, there was a typo in there before. It said .phps.

Ok, let's find that error then. The first 404 calls happen in the router, so we'll start there.

Drop this into application/libraries/MY_Router.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function MY_Router()
    {
        parent::CI_Router();
    }
    
    // --------------------------------------------------------------------
    
    /**
     * What segments do we have?
     *
     * @access    public
     */
    function _validate_request($segments)
    {
        echo '<pre>';
        print_r($segments);
        echo '</pre>';
        exit;
    }
}
// END MY_Router class

/* End of file MY_Router.php */
/* Location: ./application/libraries/MY_Router.php */
#7

[eluser]Nial[/eluser]
Thanks for all this help! That returns:

Code:
Array
(
    [0] => view
    [1] => nial
)
#8

[eluser]Pascal Kriete[/eluser]
Ok, so it's like calling example.co.uk/index.php/view/nial . So although it gets the class right, we need a function in there as well.

Try renaming the view class to something like user and give it a show function (view won't work as a method name).

Your htaccess will need to change a tad:
Code:
rewriteRule (.*) index.php/view/%1/$1 [L]

to

rewriteRule (.*) index.php/user/show/%1/$1 [L]

And the controller:
Code:
class User extends Controller {
    
    function User()
    {
        parent::Controller();        
    }

    function show($username = 'none')
    {
        echo $username;
    }
}
#9

[eluser]Nial[/eluser]
Aha, that's working! Thanks! One question: why isn't it possible to point towards user (and the index method in the user controller class), instead of user/view/?
#10

[eluser]Pascal Kriete[/eluser]
In theory it is possible, but CodeIgniter will interpret the username as a passed in function and throw a 404. You can fix it using _remap or a route, but then you're fixating the class on that one function and adding new method becomes difficult as they might clash with usernames.




Theme © iAndrew 2016 - Forum software by © MyBB