SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: 301 redirect for old dynamic index.php urls to Wordpress doesn't work

  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    301 redirect for old dynamic index.php urls to Wordpress doesn't work

    Getting a lot of server errors in my webmaster tools logs for these:

    http://www.vanilla.com/?orderby=prod...%2F2%2Fpage/2/

    I just want all the ones that start with orderby=product to point Google to:

    http://vanilla.com/shop

    I tried:
    RewriteRule ^\?orderby=product(.*)$ http://vanilla.com/shop [R=301,L]

    (It breaks if I take out the \ before the ?) But that didn't work. It redirects me to the blog page.


    Maybe part of the problem is this is a Wordpress site using permalinks and using this in the .htaccess:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress

    So then I tried the following. I put my command ABOVE the Wordpress block. And RewriteEngine On is at the very top as well.:
    RewriteRule ^index.php?orderby=product(.*)$ http://vanilla.com/shop [R=301,L]

    I'm thinking Google is recording that long url after something stripped away index.php from the path.

    And even if I do this:
    redirect 301 /index.php?orderby=product_cdate&DescOrderBy=ASC&Itemid=159&option=com_virtuemart&page=shop.browse&category_id&manufacturer_id=0&keyword&keyword1&keyword2&limit=5&limitstart=0page%2F2%2Fpage%2F8%2Fpage%2F4%2Fpage%2F2%2Fpage%2F6%2Fpage%2F3%2Fpage/3/ http://vanilla.com/shop

    It redirects to /?orderby=product_cdate&DescOrderBy=ASC&Itemid=159&option=com_virtuemart&page=shop.browse&category_id&manufacturer_id=0&keyword&keyword1&keyword2&limit=5&limitstart=0page%2F2%2Fpage%2F8%2Fpage%2F4%2Fpage%2F2%2Fpage%2F6%2Fpage%2F3%2Fpage/3/ http://vanilla.com/shop

    IT JUST REMOVES THE INDEX.PHP and doesn't redirect me anywhere.

    I guess the first order of business is to figure out why it's stripping out index.php from the string. Then try to get it to redirect, but I don't know. I'm stumped.

  2. #2
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    705
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    There's a couple problems here. First, the query string will *not* be in the path that rewrite rules match on. And second, the "?" is special in regular expressions. It means the preceding character is optional.

    This is untested, but try something like this:

    RewriteCond %{QUERY_STRING} ^orderby=product
    RewriteRule ^$ http://vanilla.com/shop [R=301,L]
    "Folks who know what they're doing make complexity seem simple."

  3. #3
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Jeff. That's a step forward. It redirects to:

    http://vanilla.com/shop/?orderby=pro...F3%252Fpage/3/

    but only if I leave ,L in there. Take that out and there is no effect. It is being trumped by what follows I guess, like this.

    RewriteCond %{QUERY_STRING} ^orderby=product
    RewriteRule ^$ http://vanilla.com/shop [R=301]

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress

    what I want is to get rid of the query string entirely so it just goes to http://vanilla.com/shop

    or maybe I should just noindex those. hmmm. ?? That's a thought too.

  4. #4
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    705
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    One extra question mark should clear that up.

    RewriteCond %{QUERY_STRING} ^orderby=product
    RewriteRule ^$ http://vanilla.com/shop? [R=301,L]
    "Folks who know what they're doing make complexity seem simple."

  5. #5
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And you're absolutely right. One little ? did the trick. Thank you!
    Do I have to worry about the L when the important Wordpress rewrite directives follow?

    How about this next one? It's harder.

    Suppose we have urls like this:
    /index.php?page=shop.product_details&product_id=113&flypage=flypage.tpl&pop=0&option=com_virtuemart&Itemid=159
    that should go to pages like this:
    http://vanilla.com/shop/pure-vanilla-dextrose-powder/

    whereby the product id differs and goes to different pages. And there are lots of variations of that url that include product ids. So any variation with a match on product_id=something should all do the same thing.

    How do you write that to work:
    RewriteCond %{QUERY_STRING} [product_id=113]
    RewriteRule [$] http://vanilla.com/shop/pure-vanilla-dextrose-powder [R=301,L]

    (Understand I don't know what I'm doing there. Just printed that to give you the idea.

    And it gets trickier because we may have product 113, 114, 115, 116 all now needing to point to the SAME NEW page. Is there a way to combine them into one directive maybe with an "or" statement?

    Since there were only about 30 or 40 products to be redirected I have no problem writing a command for each, but if there is a way to combine them, that would be great.

    Thanks.

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,310
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    k&m,

    WP protects idiots from their own demise with the <IfModule> wrappers but keeping them in your .htaccess code is TERRIBLY wasteful of the server's resources (moreso even than WP itself, I believe).

    JM's first response should have been that a RewriteRule can only examine the %{REQUEST_URI} variable which specifically excludes the protocol, domain and query string.

    Order: Your SPECIFIC mod_rewrite code must come before the WP code which is very generic - think about the generic code hijacking ALL requests and not giving your code a chance to match.

    The Last flag will direct Apache to go institute the redirection and restarts the pass through the mod_rewrite code.

    If you do not wish to retain an existing query string, there are two ways to delete it: You've discovered the first (using a ? after the redirection - it gets removed automatically so don't worry about it showing in your links) and creating a new query string.

    Making product id redirections is simple - and effective so long at their redirections are to existing web pages (else they'll be hijacked by WP's code, too).

    You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It's helped may members and should help you, too.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    Updated mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    705
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    ...keeping [<IfModule> wrappers] in your .htaccess code is TERRIBLY wasteful of the server's resources (moreso even than WP itself, I believe).
    Wow. Actually, no. Not even close.

    You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It's helped may members and should help you, too.
    Unfortunately anyone who learns from this tutorial will have to unlearn some incorrect information later on. I strongly recommend that anyone interested learn from the Apache documentation. That's where you'll find the most complete, authoritative, and correct information.
    "Folks who know what they're doing make complexity seem simple."

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •