WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] RewriteRule in .htaccess inconsistent on Multisite (6 posts)

  1. salientdigital
    Member
    Posted 2 years ago #

    I have 14 sites set up and working fine. I've been developing the new sites by pointing the domains in my local /etc/hosts file. Almost ready to launch now, so I'm finishing up by implementing rewrite rules from the prior site (it was done with ColdFusion) to the same pages on the new site.

    Trouble is, I'm finding a few things don't work properly.

    Am I correct in that, for .htaccess rules to work, they have to be added to .htaccess and also to a custom rewrite_rules_array filter?

    So far that is the only way I've been able to get them working.

    Example:

    # in .htaccess
    RewriteRule foo index.php?pagename=about [R=301,L]
    
    # in mytheme/functions.php
    function my_rewrite_rules( $rewrite_rules ) {
        $new_rules = array(
              'foo' => 'index.php?pagename=about'
        );
        $rewrite_rules = $new_rules + $rewrite_rules;
        return $rewrite_rules;
    }
    add_filter('rewrite_rules_array', 'my_rewrite_rules');

    Without both rules in both places, the rule doesn't work.

    Now here's where it gets real confusing. When I try to rewrite the old link index.cfm?fuseaction=this.that to a WP page, it fails completely. I've tried

    # in .htaccess
    RewriteRule foo index.php?pagename=about [R=301,L]
    RewriteRule index\.cfm\?fuseaction=this\.that index.php?pagename=this-that [R=301,L]
    
    # in mytheme/functions.php
    function my_rewrite_rules( $rewrite_rules ) {
        $new_rules = array(
              'foo' => 'index.php?pagename=foo',
              'index\.cfm\?fuseaction=this\.that' => 'index.php?pagename=this-that'
        );
        $rewrite_rules = $new_rules + $rewrite_rules;
        return $rewrite_rules;
    }
    add_filter('rewrite_rules_array', 'my_rewrite_rules');

    and several other forms to no avail. I just get a "WordPress 404 page" every time. In short, certain redirects appear to be working fine, others do not. So, based on the examples above, this works:
    multisite.com/foo (shows the about page as expected)
    multisite.com/index.php?pagename=this-that (shows the this-that page)
    multisite.com/this-that (shows the this-that page ... yes, I am using /%postname% for permalinks)
    multisite.com/index.cfm?fuseaction=this.that (404)

    Any suggestions?

  2. Am I correct in that, for .htaccess rules to work, they have to be added to .htaccess and also to a custom rewrite_rules_array filter?

    Generally no. Just .htaccess

    But I think there's something special about how you're handling things becuase of this:

    I've been developing the new sites by pointing the domains in my local /etc/hosts file.

    You're mapping domains locally. Are you also using a domain mapping plugin on WP's end?

  3. salientdigital
    Member
    Posted 2 years ago #

    Yes, I'm using Domain Mapper. I'm using the actual domain names in my hosts file to be able to use an identical copy of the same database that is on the production server.

  4. You shouldn't need to mess with a custom rewrite_rules_array filter, then.

    What local set up do you have? A WAMP/MAMP type stack?

  5. salientdigital
    Member
    Posted 2 years ago #

    Yep, I'm using the Apache & PHP that ship from Apple on a brand new Mac Mini, and MySQL 5.1 on a dev server on our LAN. A colleage uses MAMP Pro on a dual G5 and has verified all the same strangeness on his local box. And I've also replicated the same strangeness on my MBP 17" under Snow Leopard. And the same thing happens on our staging server running Ubuntu 10.04 LTS.

  6. salientdigital
    Member
    Posted 2 years ago #

    Thanks to this QA on Stack overflow, I figured out what I was missing before.

    If you look up the thread at my original code, I didn't realize that mod_rewrite actually doesn't see the query string at all. It only cares about the document itself. If you need to analyze the query string to make your rewrite rule function you need a RewriteCond that looks at the %{QUERY_STRING} variable.

    I'm posting the solution here in case it helps someone in the future.

    In a nutshell, what wasn't working for me were any URLs containing query string parameters. What I discovered is that it is possible to strip them out of the redirected page:

    # in .htaccess, ABOVE WP and Multisite rules
    
    Redirect 301 /foo /bar

    This works - sends a 301 and relocates to /bar
    Any url without query string parameters works fine this way.

    # in .htaccess, ABOVE WP and Multisite rules
    
    RewriteCond %{QUERY_STRING} ^var=val$ [NC]
    RewriteRule (.*) /var/val? [L,R=301]

    This works too - it sends a 301 and relocates to /var/val Note the ? after val in the rewrite rule? This little magic strips ?var=val from the resulting url. In other words, without the trailing question mark, the URL that gets redirected to is /var/val?var=val. By default, Apache tries to preserve the incoming query string parameters and pass them on to the redirected page, which, in my case, I didn't want.

    Problem solved.

Topic Closed

This topic has been closed to new replies.

About this Topic