0

Just wondering if there is a way to redirect vistors from a URL that contains member ids to a url that contains the username.

I originally had member pages set up like so... members/profile/{member_id}/ for a range of profile pages. I have since changed this to members/profile/{username}/ and I was wondering if there is a way to redirect a visors to the new URL if they were to attempt to visit a url with a member id still in it.

I tried

{if segment_3 != username}
  {redirect="members/profile/{username}/"}
{/if}

and

{if segment_3 == member_id}
  {redirect="members/profile/{username}/"}
{/if}

but neither has an effect. (The 'profile' pages are running within Switchee, so I'm not sure if thats having an effect at all?)

1

Shorn I have tested this and it works great. However because of the way the {redirect} variable works you cant use {username} in the redirect variable because it will blow up the URL so I have accomplished what you are needing with a little bit of php:

1.) In the CPanel go to Design>Template Preferences and click on the desired template group and template you are needing this functionality on on the top left of the screen. Then allow PHP yes, and let the PHP parse on the input stage.

2.) Add this code to your template.

<?php
    $uname = ee()->session->userdata('username');
?>
{if username != "{segment_3}"}
    {redirect='members/profile/<?php echo $uname; ?>'}
{/if}
0

Hmm, that seems like it should work. Have you tried either of the following?

{if '{segment_3}' == '{member_id}'}
   {redirect="members/profile/{username}/"}
{/if}

or

{if '{segment_3}' == '{logged_in_member_id}'}
   {redirect="members/profile/{logged_in_username}/"}
{/if}
1
  • Doesn't have an effect I'm afraid.
    – shorn
    Mar 24 '14 at 23:53
0

If you're using switchee, it might just be a matter of setting up a case specifically to check for member ID's, rather than using an if-check.

Member IDs are only numberic, where usernames have regular letters (and generally not numbers)

Try something along these lines:

{exp:switchee variable="{segment_3}" parse="inward"}
{case value="#^\d{1,99}$#"}
  {exp:member:custom_profile_data member_id="{segment_3}"}
  {redirect="members/profile/{username}/"}
  {/exp:member:custom_profile_data}
{/case}

{case value="" default="Yes"}
  Your regular template goes here.
{/case}
{/exp:switchee}

You're going to want to check my case value's regex - that's meant to check that the value is a number (and only a number).

5
  • I am using Switchee but I'm running the variable of segment 4, although I've just realised that switchee variables can be nested so maybe that might be the way to go! Thank you for the suggestions, I'll have a bash at trying it out (and testing the regex) and let you.
    – shorn
    Mar 24 '14 at 23:52
  • I made an error in that code above. the third line should read: {exp:member:custom_profile_data member_id="{segment_3}"} - I'm editing my answer above to include it. If you don't have that member_id piece, it'll only show the current member.
    – SamC
    Mar 25 '14 at 13:58
  • Here's an additional option, you could use a plugin like Detour Pro, and create 301/302 redirects for member-id => username.
    – SamC
    Mar 25 '14 at 19:57
  • Can you use those variables within Detour Pro? (I have it). I cannot see anything in the docs that mention variables?
    – shorn
    Mar 26 '14 at 2:05
  • I'm not sure how the wildcard function works. I was actually thinking like just doing them manually. If you've got a hundred members, maybe not the best idea. But wanted to suggest it as a semi-viable option.
    – SamC
    Mar 26 '14 at 15:36

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.