• Resolved coolplektor

    (@coolplektor)


    Hi,

    Im trying to implement “public profile” page for users who registred on website. Im using “/%postname%” custom permalink structure. So for example public page of profile should be:

    https://localhost/username (using localhost as example) (and where username for example = “john”)

    My problem is that “username” cannot be page (obviously i cannot make page for every user), and if its not page after slash (or post) it will call 404.php (which is good)

    Point is that url structure need to stay the same:
    https://localhost/pageone -> will triger page
    https://localhost/pagetwo -> will triger page ….
    https://localhost/username -> will be parametar (not page) but should work

    Does anyone have idea how to deal with this (i already tried with several url rewrites but failed).

    P.S. Implementing it like https://localhost/profile/username is really easy (adding one more page and implementing custom template for that page) but that is not what i need here.

    Tnx

Viewing 1 replies (of 1 total)
  • Thread Starter coolplektor

    (@coolplektor)

    add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
    add_filter('query_vars','wp_insertMyRewriteQueryVars');
    add_filter('init','flushRules');
    
    // Adding the var so that WP recognizes it
    function wp_insertMyRewriteQueryVars($vars)
    {
        array_push($vars, 'user');
        return $vars;
    }
    
    // Remember to flush_rules() when adding rules
    function flushRules(){
    	global $wp_rewrite;
       	$wp_rewrite->flush_rules();
    }
    
    // Adding a new rule
    function wp_insertMyRewriteRules($rules)
    {
    	$newrules = array();
    	$newrules['(?!\bfirst_escape_page\b)(?!\bsecond_escape_page\b)([A-Za-z0-9\-\_\.]+)/?$'] = 'index.php?user=$matches[1]';
    	$finalrules = $newrules + $rules;
            return $finalrules;
    }
    
    //Stop wordpress from redirecting
    remove_filter('template_redirect', 'redirect_canonical');
Viewing 1 replies (of 1 total)
  • The topic ‘Non Page parametar after / without redirect to 404.php’ is closed to new replies.