• Hey guys, I had read a lot of articles, guides & tips and still cant understand how to rewrite rule.
    Here is what i got:
    my url:

    http://localhost/wordpress/?navopt=main

    my code in functions.php:

    /*
     * Add rewrite tags
     */
    function add_new_rewrite_tags() {
        add_rewrite_tag(
                '%index%',
                '([^/]*)' ) ;
    }
    add_action( 'register_special_query_vars', 'add_new_rewrite_tags' ); /*hooked to a previous function which gets vars from the url.*/
    
    /*
     * Rewriting rules
     */
    function query_rewrite_new_rules() {
        add_rewrite_rule(
                '^/([^/]*)',
                '?navopt=$matches[1]',
                'top' );
    }
    add_action( 'add_new_rewrite_tags', 'query_rewrite_new_rules');

Viewing 4 replies - 1 through 4 (of 4 total)
  • Since you didn’t mention which articles, guides, and tips you read, I recommend starting here:

    http://codex.wordpress.org/Rewrite_API
    http://www.hongkiat.com/blog/wordpress-url-rewrite/

    You do realize you linked to your localhost WP installation, right?

    Moderator bcworkz

    (@bcworkz)

    And if you’re still confused after that, you’ll need to provide an example of the initial request URL and explain what you want to happen with each URL element before anyone can make a meaningful suggestion.

    Thread Starter almightymax

    (@almightymax)

    Ok.
    so i got 3 links:

    example.com/?navopt=main
    example.com/?navopt=filter
    example.com/?navopt=brand

    ( I got a variable named $nav to navigate through these links. it means “navopt=$nav” )
    what I want to do is to rewrite these link to this:

    example.com/main
    example.com/filter
    example.com/brand

    I read what you have posted but I cant figure it out to make it work. I tried a lot of code and still nothing. just showin me as it is or errors.
    I will be greatful if you’ll help me here.

    Moderator bcworkz

    (@bcworkz)

    This is sort of reversed from what I usually see, which may be why you’re having trouble. If your installation knows what to do with the links as rewritten to, I would not use the WP Rewrite API at all, rather use .htaccess rewrite rules directly to do the job.

    Unfortunately, I struggle myself to get .htaccess rules exactly right myself, so I’m reluctant to suggest any actual code. As you may know, one small typo in .htaccess will break your entire site, and I wouldn’t want to be responsible for that.

    My process is to search about for similar examples, which usually abound, then take a stab at it on a test server. Then it’s trial and error, using error and access logs as a guide to what went wrong. It seems something always goes wrong when I do this, several times.

    Despite this, I think you may have better luck with this approach than the WP Rewrite API. Sorry I can’t be of more help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘cant understand how to rewrite url’ is closed to new replies.