Welcome Guest, Not a member yet? Register   Sign In
Lovely User URLS (http://domain.net/~Username)
#1

[eluser]IamPrototype[/eluser]
How can I make URLS like http://domain.net/~Username, http://domain.net/username etc., and make it redirect to a users profile. I know CI is build with routes, but do I have to do some reg. exp or? I hope somebody will help me (it's for my community). Smile
#2

[eluser]freshface[/eluser]
Or use remap in your controller

function _remap()
{
$usr = $this->uri->segement(1);

if($usr)
{
redirect('…');
}
else
{
echo 'no username passed';
}
}
#3

[eluser]Phil Sturgeon[/eluser]
It wouldnt get to the controller, as the username would be used as the controller name and a 404 would be returned.

You could use routes, but then EVERY 1 uri segment will be checked as a username.

The way I like to do this is to add a URL suffix in the config file (meaning add .html or similar to all your pages) then use the following mod)_rewrite rules:

Code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9_-]+)$
RewriteRule ^(.*)$ index.php/profiles/view/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

That is basically a standard CI .htaccess with an extra rule in it. Any URI segment 1 with a-z, 0-9, - or _ in it which does NOT have a URL suffix will go to a controller which will take the username and show the correct profile.

If you have mod_proxy enabled you could even swap the flag [L] with [L,P] to make the URL stay as example.com/username.
#4

[eluser]IamPrototype[/eluser]
Where do I place it?

So it has to be like:

Code:
function _remap() {

$username = $this->uri->segment(1);

if($username) {

redirect('users/profile/' . $username . ');

}

}

I don't need an else-statement on my if(), because I only want the user to redirect IF the URL is something like http://domain.net/Username
#5

[eluser]IamPrototype[/eluser]
[quote author="pyromaniac" date="1232390860"]It wouldnt get to the controller, as the username would be used as the controller name and a 404 would be returned.

You could use routes, but then EVERY 1 uri segment will be checked as a username.

The way I like to do this is to add a URL suffix in the config file (meaning add .html or similar to all your pages) then use the following mod)_rewrite rules:

Code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9_-]+)$
RewriteRule ^(.*)$ index.php/profiles/view/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

That is basically a standard CI .htaccess with an extra rule in it. Any URI segment 1 with a-z, 0-9, - or _ in it which does NOT have a URL suffix will go to a controller which will take the username and show the correct profile.

If you have mod_proxy enabled you could even swap the flag [L] with [L,P] to make the URL stay as example.com/username.[/quote]

Thanks, I guess this example is better for use.

So everything I have to do is write that into my .htaccess and then it should work by itself? Smile

Etc http://domain.net/IamPrototype turns into http://domain.net/profiles/view/IamPrototype
#6

[eluser]freshface[/eluser]
Inside your controller.
#7

[eluser]freshface[/eluser]
What about routes?

$route['^(.*)$'] = "user/view/$1";
#8

[eluser]IamPrototype[/eluser]
That route would do it like this:

http://domain.net/IamPrototype -> user/view/IamPrototype

Without any more code?
#9

[eluser]Phil Sturgeon[/eluser]
Yes IamProtoype, all you need is to set yourself a url suffix and add that .htaccess. Nothing else required.

Blogged Smile
#10

[eluser]IamPrototype[/eluser]
Thanks a lot Smile




Theme © iAndrew 2016 - Forum software by © MyBB