• Hi everyone,

    I have created a rewrite rule that work fine on a first website.
    I have used the same rule on a second website, but instead of rewrite, WP is redirecting…
    The main difference between the 2 websites is the second is a multisites. I don’t know if it make a difference for rewriting rules.

    Here is my code in functions.php :

    function URLbateau( $query_vars ){
    	$query_vars[] = 'bateau';
    	$query_vars[] = 'gamme';
    	return $query_vars;
    }
    add_filter( 'query_vars', 'URLbateau' );
    
    function rewritebateau() {
    	global $wp_rewrite;
    	add_rewrite_rule(
    		'^les-bateaux/(.*)/(.*)?',
    		'index.php?pagename=bateaux&bateau=$matches[2]&gamme=$matches[1]',
    		'top'
    	);
    }

    So, when I display an URL like: /les-bateaux/gamme-premium/tarpon-49-qp
    I’m redirected to /bateaux/

    An idea?
    Thanks!
    Laurent

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    pagename=bateaux tells WP to fetch that page, which is different than the request. Thus a redirect is necessary. If the requested page can remain unchanged and the rewrite rule only sets the query string vars, then a redirect is unnecessary.

    Thread Starter laurent53

    (@laurent53)

    Thanks bcworkz.
    It’s a rewrite rule, it should only rewrite and not redirect, isn’t it?
    And it works on the other website…

    How can I keep my URL (ex.:/les-bateaux/gamme-premium/tarpon-49-qp) and not to be redirected?
    I don’t understand what do you mean by “If the requested page can remain unchanged”.

    Thanks!
    Laurent

    Moderator bcworkz

    (@bcworkz)

    I’ve investigated in more detail. You’re right, pagename=bateaux as the rewritten query string should not result in a redirect like I thought it would. What is the location header being sent that is causing the redirect? You can get it from the browser’s network developer tool by clicking on the initial /les-bateaux/gamme-premium/tarpon-49-qp request in the list. It’ll be in the response headers. Also, what is the returned status code?

    I suspect some other rule causing a redirect besides your rewrite rule.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirecting instead of rewriting’ is closed to new replies.