Forums

Redirect if url is http://siteurl/username (4 posts)

  1. iyoworks
    Member
    Posted 7 months ago #

    I'm working on a client site in which students will have 'portfolios'. The portfolio page is just a page that pulls a user's projects (my custom post type).

    The url structure for the portfolios is http://siteurl/portfolios/?username

    What I want is for http://siteurl/username to redirect to http://siteurl/portfolios/?username

    Does anyone have any idea on how to get this setup?

  2. Rebecca Mastey
    Member
    Posted 7 months ago #

    Two ways to do it
    1. Download the Page Link To plugin, create a page for each student and set their portfolio as the redirect link <-- easy!
    2. Create a custom redirect option in .htaccess/wp_rewrite to handle this <-- hard-ish

    Depending on how many students there are, I'd honestly go with the easy option.

  3. iyoworks
    Member
    Posted 7 months ago #

    Thanks. The site will probably start with 30-50 students and will grow as more student go through the program. I'm not too versed with the wp_rewrite tag or the .htaccess file. How would I get the second option to work?

  4. iyoworks
    Member
    Posted 7 months ago #

    I created a work around. This is the code I'm using in my functions.php file:

    //redirect to student profile if [homeurl]/[username] is the current url.
    if (!is_admin()) {
    
    $url = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    $url = ereg_replace("(https?)://", "", $url);
    $token = explode('/', $url);
    $token = $token[sizeof($token)-1];
    
    $shortPortfolioUrl = ereg_replace("(https?)://", "", (home_url(''.$token.'')));
    if ($url == $shortPortfolioUrl) {
    	if (username_exists($token) != null ) {
    		$location = home_url().'/portfolios/?'.$token;
    		wp_redirect( $location );
    		exit;
    	}
    }
    
    }

Reply

You must log in to post.

About this Topic

Tags