Welcome Guest, Not a member yet? Register   Sign In
Using .htaccess for dynamic subdomains
#11

[eluser]Phil Sturgeon[/eluser]
Just put the extra rule in below the rest. The L after the 301 redirect flag means it is the last rule apache will look at when it matches it. So it see's that flag, then redirects and runs through the .htaccess again.

Code:
<IfModule mod_rewrite.c>
# Redirect to user blog (with any trailing path)
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/blog/user/$1$2 [R=301,L]

## Otherwise, force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !\.(css│js│jpg│jpeg│gif)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]

</IfModule>

I removed a condition from your bottom rule-set as it was duplicating logic. You dont need to specify folders to be ignored if you are making sure the files exist as well. This is more flexible.

Note: I have mod_rewrite on my dev box now, but I am not on it, so this is once again untested :-p
#12

[eluser]Phil Sturgeon[/eluser]
Don't forget you will need to set up virtual hosts so that your sub-domains all point to the main site. Otherwise this will 404.

You could perhaps set index.php as the 404 page to send these 404's the right way if you do not have the access to make virtual hosts, but this would mess with server stats quite a bit.
#13

[eluser]_sty_[/eluser]
Thanks a lot for assisting guys! I have one more question left. How to rewrite those htaccess instrusctions so that redirected url won't be obviously shown in an address string?
I.e. when redirect occurs I can see the real url but I don't want. It should be hidden like for livejournal's accounts.

Thanks beforehand
#14

[eluser]_sty_[/eluser]
up
#15

[eluser]Watermark Studios[/eluser]
Okay, I've followed the example above and I'm still getting the default router for CI loading. htaccess seems to be skipping all of my subdomain rules. So, when I put in "username.example.com" it is loading the default controller. This is what I have so far:

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    <IfModule mod_proxy.c>
      # Redirect to user blog (with any trailing path)
      RewriteCond %{HTTP_HOST} !^www. [NC]
      RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
      RewriteRule ^(.*)$ http://www.example.com/user_public/index/$1$2 [P,L]
    </IfModule>

    <IfModule !mod_proxy.c>
      # Redirect to user blog (with any trailing path)
      RewriteCond %{HTTP_HOST} !^www. [NC]
      RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
      RewriteRule ^(.*)$ http://www.example.com/user_public/index/$1$2 [R=301,L]
    </IfModule>

    ## Otherwise, force www;
    RewriteCond %{HTTP_HOST} ^example.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

I have my Apache conf.d/vhost.conf file listing:
Code:
ServerAlias www.example.com example.com *.example.com

I also have my A record for *.example.com pointed to this web server.

I want:

username.example.com -> www.example.com/user_public/index/username

My index method of the user_public controller accepts two parameters ( $username = null, $stage = 1 ), which in turn loads the appropriate stage controller and passing that controller the username. Then I could just have a link in the first stage pointing to the second stage:

username.example.com/2 -> www.example.com/user_public/index/username/2

Which would instruct the index controller to load the stage 2 controller. I want to do it through mod_rewrite because any other url (www.example.com) I want to work just the way CI intended. I'm just trying to make it easy on my clients to remember their personal url.

What am I missing?
#16

[eluser]Watermark Studios[/eluser]
Wow! I thought mod_rewrite would handle it for me, but I was sorely mistaken. All I had to do was add what DPrevite suggested. I did this and it worked like a charm!

Code:
// May not be the best place to put this, but...
$URL = explode('.', $_SERVER['HTTP_HOST']);
$config['subdomain'] = $URL[0];
unset($URL);

$config['base_url'] = 'http://' . $config['subdomain'] . '.example.com';

Thank you all!




Theme © iAndrew 2016 - Forum software by © MyBB