0

I have been trying to redirect the default user url of '/user' to the url alias of '/users/username'.

If i go to '/user/uid' it redirects properly but if i type in '/user' the url doesn't redirect.

Using hook_preprocess_user_profile() I can print arg(0) and arg(1) with drupal_set_message() and it display that arg(1) exists as the uid on '/user'.

I have tried

function mymodule_preprocess_user_profile(&$vars) {
  global $user;
  //print_r($vars);
  if (arg(0) == 'user' && arg(1) == $user->uid){
    //drupal_set_message('0'.arg(0).'1'.arg(1));
    drupal_goto("users/$user->name");
  }
}

but this end up in an endless redirect. Is it possible to even accomplish this?

1

By redirecting /user you'll be redirecting the login page to a user page. When someone goes to /user and logs in they are automatically redirected to /user/username. Unless you have login disabled(not advised) I can't see what you want to achieve, can you explain further?

Redirects are best handled by the path redirect module

or using rules in .htacces or etc/apache2/httpd.conf

  • I think what the OP is trying to do is make /user redirect /user/username when a user is logged in. Currently if you're logged in /user == /user/logged-in-users-name. – filmoreha Apr 19 '12 at 2:28
0

After some testing, I realise if you visit /user, the $_GET['q'] (which produces the arg's) returns /user/uid, so it looks like you are on the /user/uid page even though you are not.

I placed the code below into my themes template.php and it will redirect /user to /user/uid. If you have URL aliases enabled, this will then redirect to that.

function MYTHEME_preprocess_page(&$page) {

  if (request_uri() == base_path().'user' && $page['user']->uid != 0 ) {
    $path = 'user/' . $page['user']->uid;
    drupal_goto('user/1');
  }
}

I wanted to do the same on my site, but I use the me-alias module. The following code worked in my themes template.php. Re the above problem of /user looking like /user/uid, this is okay in this case because I am going to a new path (/user/me), instead of being stuck in the /user/uid loop. This also changes all the /user/uid/* pages too.

function MYTHEME_preprocess_page(&$page) {

  $path = $_GET['q'];
  if (arg(0) == 'user' && arg(1) == $page['user']->uid) {
    $path = str_replace('user/' . arg(1), 'user/me', $path);
    drupal_goto($path);
  }
}
0

You can redirect by using trigger. Drupal provide you basic functionality to do the changes after login by few steps..

  1. Open with your admin by given url : admin/config/system/actions
  2. Chose from CREATE AN ADVANCED ACTION and select Redirect to URL..
  3. Type Label like - Redirect to URL - users #
  4. URL - user
  5. Now enable module triger in admin/modules page.
  6. Go to - admin/structure/trigger/user
  7. Choose an action from TRIGGER: AFTER A USER HAS LOGGED IN
  8. And assign to Redirect to URL - users #

Now these 8 steps will short out your problem. Logout and again login to check functionality.

Thanks.. Vishal Nigam

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.