• Resolved haxxxton

    (@haxxxton)


    I am trying to create ‘faux’ pages that all link to the homepage with associated variables in the GET data. For example:

    https://mysite.com/img/IMGNAME/comment/5678/
    provide data like:
    https://mysite.com/?image=IMGNAME&comms=5678

    I have the following in my htaccess file:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^img/([^/]+)/comment/([^/]+) /?image=$1&comms=$2 [L,NS]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    the issue is that i have a post that has a slug of IMGNAME (this is how im finding the post given the GET variable on the homepage).

    When I manually enter https://mysite.com/?image=IMGNAME&comms=5678 it all works great.

    When I enter https://mysite.com/img/IMGNAME/comment/5678/ I am redirected to (the url address changes AND it renders the single.php template) https://mysite.com/category/IMGNAME/

    I am assuming index.php (or ‘/’) then gets processed by wordpress, in which it attempts to find the post with the closest name to the content of the variable im handing it and REDIRECTS to the post?

    Does anyone know how to have the entered url STAY in the address bar and allow the homepage to process the GET variables?

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

    (@haxxxton)

    I found rather then doing the rewriting in .htaccess you should add new custom rewrite rule wordpress way. I added the following to my functions.php file

    function my_custom_rewrites(){
            add_rewrite_rule(
                'img/([^/]+)/comment/([^/]+)',
                'index.php?page_id=130&image=$matches[1]&comms=$matches[2]',
                'top' );
            add_rewrite_rule(
                'img/([^/]+)',
                'index.php?page_id=130&image=$matches[1]',
                'top' );
        }
        add_action( 'init', 'my_custom_rewrites' );
    
        function my_custom_rewrite_tags(){
            add_rewrite_tag('%image%','([^&]+)');
            add_rewrite_tag('%comms%','([^&]+)');
        }
        add_action( 'init', 'my_custom_rewrite_tags' );

    Then in my template file i can reference the tags with the following:

    $image = false;
        if($wp_query->query_vars['image']){
            $image = $wp_query->query_vars['image'];
        }
        $comms = false;
        if($wp_query->query_vars['comms']){
            $comms = $wp_query->query_vars['comms'];
        }

    i also tried the tips shared here https://docs.dev4press.com/tutorial/practical/debug-wordpress-rewrite-rules-matching/ that might helped understand which Rewrite rule is matching the url and why a particular template is being rendered.

    It’s worth mentioning that any time you change your rewrite rules you need to flush them using something like flush_rewrite_rules(true) or by going into options->permalinks and just hitting ‘save’

Viewing 1 replies (of 1 total)
  • The topic ‘Custom htaccess, rewrite being overwritten by redirect’ is closed to new replies.