Question

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

http://example.com/img/IMGNAME/comment/5678/

provide data like: http://example.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 http://example.com/?image=IMGNAME&comms=5678 it all works great.

When I enter http://example.com/img/IMGNAME/comment/5678/

I am redirected to http://example.com/category/IMGNAME/ (the url address changes AND it renders the single.php template)


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?

Do i need to create a different version of the base index.php file to have it not redirect/search for similarly named posts? I still need it to process my theme template pages with wordpress tags etc.

Was it helpful?

Solution

I think rather then doing the rewriting in .htaccess you should add new custom rewrite rule wordpress way.

function my_custom_rewrites(){
    add_rewrite_rule(
        'img/(.*)/comment/(.*)',
        'index.php?image=$matches[1]&comms=$matches[2]',
        'top' );
}
add_action( 'init', 'my_custom_rewrites' );

You can also try the tips shared here http://docs.dev4press.com/tutorial/practical/debug-wordpress-rewrite-rules-matching/ that might help you understand which Rewrite rule is matching the url and why a particular template is being rendered.

Your URL might very well be matching with other patterns of rewrite rules of Wordpress. So registering your own rule in wordpress gives you the opportunity to get it checked before the core rules, Notice the third parameter of the add_rewrite_rule which says 'top', this specify the same

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top