Welcome Guest, Not a member yet? Register   Sign In
Routing URL && Sub domains
#1

[eluser]IronicNet[/eluser]
Hi!
Im'm quite new at Ci, but since i saw the videos and the documentation (great work!) i fell in love immediatly!... (and i come from a .net world!)

Well, i'm having the following problem...

I have an application which each user can see their profile... the way to do this is the following:
Quote:http://www.domain.com/username

I used to do it via .htaccess, but now im trying to do it the CI way!...

i have the following controller :
(user.php)
Code:
<?php

class User extends Controller {

    function User()
    {
        parent::Controller();    
    }
    function view()
    {
        echo "View";
    }
    
    function index()
    {
        echo "Index";
    }
}
?>
and this is the routing file:
(routes.php)
Code:
$route['default_controller'] = "user";
$route['scaffolding_trigger'] = "";
$route['([a-zA-Z0-9_-]+)'] = "user/view/$1";

if i type "www.domain.com" it shows "Index"...
but if i type "www.domain.com/someusername" it throws a 404 error.
but if i type "http://www.domain.com/index.php/username" it shows me "View".

Any suggestions? some help?

I'm sorry, i didn't find any post with this... or i tried the wrong keywords...
#2

[eluser]Crazy88s[/eluser]
Hi,

I come from a .NET world too and am learning my way around CI. I'll assume you are using Windows. You'll need to use a .htaccess file.

Here's what I did.

My application resides in a folder under my htdocs directory. I created a .htaccess file with the following contents:

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /CommunityRoute
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    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 then modified the httpd.conf file and removed the # from the line:
LoadModule rewrite_module modules/mod_rewrite.so

You'll need to restart apache after this.

In the config.php file for my CI application I changed the following:

Code:
$config['base_url'] = "http://localhost/myCIFolder/";
$config['index_page'] = "";

That should do it.
#3

[eluser]barbazul[/eluser]
@IronicNet: Unfortunately "the CI way" still doesn't get rid of the .htaccess file... you'll need it to make your examples to work.
the router.php file is extremely helpful but what it doesn't replace the .htaccess, it only works on the url segment that comes after the index.php

What Crazy88s says is totally correct, but I still feel like making it a little clearer for you since you come from a different universe (I mean .NET)

So if you dont use .htaccess your urls will work with this syntax:
http://www.domain.com/index.php/controller/method/parameters

if you use .htaccess to replace (.*) to index.php$1 they will work with this other syntax
http://www.domain.com/controller/method/parameters

which will (internally) be rewritten to look like the first example.

The resulting url is basically calling index.php and setting a server variable with the string that comes after index.php. This is the string that's analysed by the routing mechanism to extract the appropiate controller and method and it is on this string that any rules specified on router.php will be applied.

I hope you find this helpful.
#4

[eluser]IronicNet[/eluser]
This is my final .htaccess
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php*|robots\.txt)
    RewriteRule ^(.*)$ index.php/user/view/$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>

It's working perfectly (i erased the RewriteBase /CommunityRoute)... and i will refine it.
But it's a great example.
#5

[eluser]IronicNet[/eluser]
I forgot to add something initially...

i want that when a user enter into
Code:
http://account.domain.com
internally be redirected to
Code:
www.domain.com/account/ -or- www.domain.com/index.php/account/

Is there anyway to accomplish this? via dns or .htaccess?

or should i create another physical path to achieve this?
#6

[eluser]gr0uch0mars[/eluser]
I'm using the htaccess rewrite rules to avoid having "index.php" in my URL and it works perfectly. Now the problem comes after having validated a form, it redirects me to "index.php/form" instead "/form". I'm new in this, and I have a lot to learn, but i would appreciate anyone telling me how to solve this, thanks.
#7

[eluser]Michael Ekoka[/eluser]
@IronicNet
You may wanna add something like this to your .htaccess
Code:
RewriteCond %{HTTP_HOST} ^(account|admin).domain.com$
RewriteRule (.*) /index.php/%1/$1 [L]


@Sherlock_Brett
We might need to see what you have in your .htaccess
#8

[eluser]gr0uch0mars[/eluser]
This is what I have:
Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|phpmyadmin)
RewriteRule ^(.*)$ /index.php/$1 [L]

Something like what User Guide says.




Theme © iAndrew 2016 - Forum software by © MyBB