views:

581

answers:

4

Hello,

I just did a redesign for www.wildchildclothes.com which has a new URL structure. The old site ran on Zen Cart and had a bad URL structure. Here are some examples of what I want to redirect from:

OLD: www.wildchildclothes.com/page.html?chapter=1&id=21 NEW: www.wildchildclothes.com

and...

OLD: www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=13 NEW: www.wildchildclothes.com/customer-service.html

Here is what's in my .htaccess to achieve this:

RewriteEngine On
RewriteBase / 
RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html http://www.wildchildclothes.com? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=13(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com/customer-service.html? [L,R=301]

But this doesn't work at all - I only get sent to my 404 page instead of begin redirected. Can anyone shed some light? The full .htaccess is posted below.

Thanks much, Jonah

A: 

It would be easier and far better if you use PHP for the redirect. You could, for example, rewrite such requests to a PHP script that analyzes the URL arguments and redirects to the correct page. Maybe something like this:

// old-to-new.php
$rules = array(
    array(
        array('chapter' => '0', 'id' => '23'),  // old URL parameters
        '/customer-service.html'                // new URL
    ),
    // …
);
foreach ($rules as $rule) {
    if (array_intersect_assoc($rule[0], $_GET) == $rule[0]) {
        header('Location: http://example.com'.$rule[1], true, 301);
        exit;
    }
}
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
exit;

And the corresponding RewriteRule:

RewriteRule ^page\.html$ old-to-new.php [L]
Gumbo
Jonah
@Jonah: Well, it works for me.
Gumbo
Jonah
Gumbo
Jonah
@Jonah: Maybe you’re missing something mandatory again: Do you use `RewriteEngine on`?
Gumbo
Yep, I tried that.
Jonah
@Jonah: Do you use any other rules that might get in conflict with mine?
Gumbo
See below, I just posted the full .htaccess
Jonah
A: 

You want to allow more than one character in the query parameters. Change [^&]& to [^&]*&:

RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html$ http://www.wildchildclothes.com? [L,R=301]

RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=12(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com? [L,R=301]
Jeremy Stein
Thanks Jeremy but this still does not work. It just takes me to my 404 page. Any idea what else could be wrong?
Jonah
Did you add `RewriteEngine On`? Can you do a simple redirect like `RewriteRule . http://google.com`?
Jeremy Stein
RewriteEngine is On, could it have anything to do with DirectoryIndex index.html index.php?
Jonah
No, `DirectoryIndex` shouldn't make a difference. Again, does `RewriteRule . http://google.com` work?
Jeremy Stein
Sorry, yes it does work.
Jonah
Here is a demo that works for me: http://steindevelopment.com/wildchildclothes/demo.html
Jeremy Stein
Yeah, looks good and thanks for going to all the trouble in helping me and setting up examples - I don't know what the problem could be for me... I inserted the exact same code as you're using (with a dif. rewrite base) but it does not work. I'm about ready to give up though. I have most of our links redirected and it doesn't feel worth it to spend hours on 20 or so links that won't redirect. I have other conditions and rules in my .htaccess so this may be conflicting as Gumbo suggested above...
Jonah
I just posted the full .htaccess for reference.
Jonah
@Jonah, please start with just my .htaccess and see if that works. Then, piece by piece, add in the other parts you want and see where it breaks.
Jeremy Stein
I see the logic in that, but the site is live right now.
Jonah
You can't try it in a subdirectory or on a testing site?
Jeremy Stein
Jonah
Figure out exactly which part is breaking it. Start with redirecting on all URLS. Then add page.html. Then add one RewriteCond. If that breaks, try simplifying it. Find the smallest possible change you have to make to go from a .htaccess that works to one that doesn't.
Jeremy Stein
+1  A: 

Here is the full .htaccess for reference:

ExpiresActive on
ExpiresDefault A0

DirectoryIndex index.html index.php

<IfModule mod_rewrite.c>
RewriteEngine on
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} .*\/catalog\/.*
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

</IfModule>

redirect 301 /accessoriesmisc-c-72.html http://www.wildchildclothes.com/accessories-1-2.html
redirect 301 /circo-p-225.html http://www.wildchildclothes.com/toys.html
redirect 301 /dressesskirts-c-76.html http://www.wildchildclothes.com/girls/0-18-months/dresses.html
redirect 301 /legginglong-john-p-200.html http://www.wildchildclothes.com/girls/girls-legging.html
redirect 301 /nowali-tights-p-215.html http://www.wildchildclothes.com
redirect 301 /moose-sweatshirt-p-193.html http://www.wildchildclothes.com/boys/sweatshirt.html
redirect 301 /mushroom-hat-p-218.html http://www.wildchildclothes.com/boys/boys-mushroom-hat.html
redirect 301 /fleece-jumpsuit-p-205.html http://www.wildchildclothes.com
redirect 301 /hand-painted-clothes-c-88.html http://www.wildchildclothes.com
redirect 301 /new-baby-gifts-c-89.html http://www.wildchildclothes.com/newborns/0-6-months/gift-sets-gift-items.html
redirect 301 /hot-pink-flower-growth-chart-p-240.html http://www.wildchildclothes.com/toys/eeboo-growth-chart-flower.html
redirect 301 /sweatpants-p-201.html http://www.wildchildclothes.com/boys/sweatpant.html
redirect 301 /little-leaf-house-p-230.html http://www.wildchildclothes.com/toys/haba-little-leaf-house.html
redirect 301 /girls-jumpsonesies-c-66_82.html http://www.wildchildclothes.com/girls.html
redirect 301 /painted-dinosaur-tee-p-301.html http://www.wildchildclothes.com
redirect 301 /girls-dressesskirts-c-66_81.html http://www.wildchildclothes.com/girls/0-18-months/dresses.html
redirect 301 /girls-tops-c-66_79.html http://www.wildchildclothes.com/girls.html
redirect 301 /boys-bottoms-c-65_68.html http://www.wildchildclothes.com/boys.html
redirect 301 /counting-birds-wall-cards-p-244.html http://www.wildchildclothes.com/toys/eeboo-wall-cards-counting-birds.html
redirect 301 /footwear-c-65_78.html http://www.wildchildclothes.com
redirect 301 /long-sleeve-dress-p-209.html http://www.wildchildclothes.com/girls/long-sleeve-pocket-dress.html
redirect 301 /biker-short-p-196.html http://www.wildchildclothes.com/girls/biker-short.html
redirect 301 /on-sale-c-90.html http://www.wildchildclothes.com
redirect 301 /boys-jumpsonesies-c-65_73.html http://www.wildchildclothes.com/boys.html
redirect 301 /mimi-the-sardine-apron-p-221.html http://www.wildchildclothes.com/accessories-1-2/mimi-the-sardine-apron.html
redirect 301 /tromper-p-206.html http://www.wildchildclothes.com
redirect 301 /long-sleeve-baby-dress-and-hat-set-p-306.html http://www.wildchildclothes.com/newborns/long-sleeve-dress-with-hat-set.html
redirect 301 /faqs_all.html http://www.wildchildclothes.com/customer-service.html
redirect 301 /site_map.html http://www.wildchildclothes.com/sitemap.html
redirect 301 /boys-accessories-c-65_70.html http://www.wildchildclothes.com/accessories-1-2.html
redirect 301 /outfits-c-85.html http://www.wildchildclothes.com
redirect 301 /gv_faq.html http://www.wildchildclothes.com/customer-service.html
redirect 301 /girls-accessories-c-66_83.html http://www.wildchildclothes.com/accessories-1-2.html
redirect 301 /products_new.html http://www.wildchildclothes.com
redirect 301 /dog-fleece-jumpsuit-p-208.html http://www.wildchildclothes.com
redirect 301 /toys-eeboo-c-71_87.html http://www.wildchildclothes.com/toys/eeboo.html
redirect 301 /bogtog-pants-p-197.html http://www.wildchildclothes.com/boys/bog-tog-pants.html
redirect 301 /tops-c-74.html http://www.wildchildclothes.com
redirect 301 /ruffled-tee-p-192.html http://www.wildchildclothes.com/girls/short-sleeve-curly-tee.html
redirect 301 /tops-c-74.html http://www.wildchildclothes.com
redirect 301 /robeez-navy-rodeo-star-shoes-p-265.html http://www.wildchildclothes.com
redirect 301 /monkey-sweatshirt-p-185.html http://www.wildchildclothes.com
redirect 301 /hooded-cardigan-p-180.html http://www.wildchildclothes.com/girls/girls-hooded-cardigan.html
redirect 301 /robeez-purple-butterfly-shoes-p-249.html http://www.wildchildclothes.com
redirect 301 /painted-monster-tee-p-299.html http://www.wildchildclothes.com
redirect 301 /flapsi-p-228.html http://www.wildchildclothes.com/toys/haba-flapsi-rattle.html
redirect 301 /morning-drew-jewelry-box-p-236.html http://www.wildchildclothes.com
redirect 301 /girls-c-66.html http://www.wildchildclothes.com/girls.html
redirect 301 /moose-fleece-jumpsuit-p-207.html http://www.wildchildclothes.com
redirect 301 /jumpersonesies-c-77.html http://www.wildchildclothes.com
redirect 301 /toys-c-71.html http://www.wildchildclothes.com/toys.html
redirect 301 /short-sleeve-fitted-tee-p-191.html http://www.wildchildclothes.com/girls/girls-short-sleeve-fitted-tee.html
redirect 301 /thermal-blanket-p-216.html http://www.wildchildclothes.com/newborns/blanket.html
redirect 301 /toys-haba-c-71_86.html http://www.wildchildclothes.com/toys/haba.html
redirect 301 /long-sleeve-fitted-tee-p-187.html http://www.wildchildclothes.com/boys/boys-long-sleeve-fitted-tee.html
redirect 301 /robeez-cream-puppy-shoes-p-263.html http://www.wildchildclothes.com
redirect 301 /boys-tops-c-65_67.html http://www.wildchildclothes.com/boys.html
redirect 301 /dream-journey-p-223.html http://www.wildchildclothes.com/toys/haba-dream-journey.html
redirect 301 /boys-c-65.html http://www.wildchildclothes.com/boys.html
redirect 301 /bottoms-c-75.html http://www.wildchildclothes.com
redirect 301 /girls-bottoms-c-66_80.html http://www.wildchildclothes.com/girls.html
redirect 301 /salto-p-224.html http://www.wildchildclothes.com/toys/haba-salto-rattle.html
redirect 301 /shippinginfo.html http://www.wildchildclothes.com/shipping-information.html
redirect 301 /short-sleeve-boxy-tee-p-188.html http://www.wildchildclothes.com/boys/short-sleeve-boxy-tee.html
redirect 301 /skull-cap-p-212.html http://www.wildchildclothes.com/newborns/baby-hat.html
redirect 301 /gift-certificate-p-220.html http://www.wildchildclothes.com/index.php?dispatch=gift_certificates.add
redirect 301 /robeez-pastel-pink-flower-patch-shoes-p-253.html http://www.wildchildclothes.com
redirect 301 /animal-parade-wall-cards-p-242.html http://www.wildchildclothes.com/toys/eeboo-wall-cards-animal-parade.html

RewriteEngine On
RewriteBase / 
RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html http://www.wildchildclothes.com? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=13(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com? [L,R=301]

RewriteEngine On 
RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:[email protected] [OR] 
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR] 
RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Download\ Demon [OR] 
RewriteCond %{HTTP_USER_AGENT} ^eCatch [OR] 
RewriteCond %{HTTP_USER_AGENT} ^EirGrabber [OR] 
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR] 
RewriteCond %{HTTP_USER_AGENT} ^EmailWolf [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Express\ WebPictures [OR] 
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro [OR] 
RewriteCond %{HTTP_USER_AGENT} ^EyeNetIE [OR] 
RewriteCond %{HTTP_USER_AGENT} ^FlashGet [OR] 
RewriteCond %{HTTP_USER_AGENT} ^GetRight [OR] 
RewriteCond %{HTTP_USER_AGENT} ^GetWeb! [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Go!Zilla [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Go-Ahead-Got-It [OR] 
RewriteCond %{HTTP_USER_AGENT} ^GrabNet [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Grafula [OR] 
RewriteCond %{HTTP_USER_AGENT} ^HMView [OR] 
RewriteCond %{HTTP_USER_AGENT} HTTrack [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} ^Image\ Stripper [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Image\ Sucker [OR] 
RewriteCond %{HTTP_USER_AGENT} Indy\ Library [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} ^InterGET [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Internet\ Ninja [OR] 
RewriteCond %{HTTP_USER_AGENT} ^JetCar [OR] 
RewriteCond %{HTTP_USER_AGENT} ^JOC\ Web\ Spider [OR] 
RewriteCond %{HTTP_USER_AGENT} ^larbin [OR] 
RewriteCond %{HTTP_USER_AGENT} ^LeechFTP [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Mass\ Downloader [OR] 
RewriteCond %{HTTP_USER_AGENT} ^MIDown\ tool [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Mister\ PiX [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Navroad [OR] 
RewriteCond %{HTTP_USER_AGENT} ^NearSite [OR] 
RewriteCond %{HTTP_USER_AGENT} ^NetAnts [OR] 
RewriteCond %{HTTP_USER_AGENT} ^NetSpider [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Net\ Vampire [OR] 
RewriteCond %{HTTP_USER_AGENT} ^NetZIP [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Octopus [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Explorer [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Navigator [OR] 
RewriteCond %{HTTP_USER_AGENT} ^PageGrabber [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Papa\ Foto [OR] 
RewriteCond %{HTTP_USER_AGENT} ^pavuk [OR] 
RewriteCond %{HTTP_USER_AGENT} ^pcBrowser [OR] 
RewriteCond %{HTTP_USER_AGENT} ^RealDownload [OR] 
RewriteCond %{HTTP_USER_AGENT} ^ReGet [OR] 
RewriteCond %{HTTP_USER_AGENT} ^SiteSnagger [OR] 
RewriteCond %{HTTP_USER_AGENT} ^SmartDownload [OR] 
RewriteCond %{HTTP_USER_AGENT} ^SuperBot [OR] 
RewriteCond %{HTTP_USER_AGENT} ^SuperHTTP [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Surfbot [OR] 
RewriteCond %{HTTP_USER_AGENT} ^tAkeOut [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Teleport\ Pro [OR] 
RewriteCond %{HTTP_USER_AGENT} ^VoidEYE [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Web\ Image\ Collector [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Web\ Sucker [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebAuto [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebCopier [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebFetch [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebGo\ IS [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebLeacher [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebReaper [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebSauger [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Website\ eXtractor [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Website\ Quester [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebStripper [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebWhacker [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WebZIP [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Wget [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Widow [OR] 
RewriteCond %{HTTP_USER_AGENT} ^WWWOFFLE [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Xaldon\ WebSpider [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Zeus 
RewriteRule ^.* - [F,L]

# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP
Jonah
Please edit your initial question.
Gumbo
I just edited the initial question.
Jonah
+2  A: 

None of the above solutions worked for me, but this does:

To redirect www.wildchildclothes.com/page.html?chapter=1&id=21 to www.wildchildclothes.com:

RewriteCond %{query_string} chapter=1&id=21
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]

To redirect www.wildchildclothes.com/index.php?main_page=document_product_info&products_id=280 to www.wildchildclothes.com:

RewriteCond %{query_string} main_page=document_product_info&products_id=280
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]

To redirect www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=9 to www.wildchildclothes.com/customer-service.html:

RewriteCond %{query_string} main_page=faq_info&fcPath=0&faqs_id=9
RewriteRule (.*) http://www.wildchildclothes.com/customer-service.html? [R=301,L]

I'm sure there are better ways to do this but this works for me and .htaccess just gives me a headache so I'm not digging any further.

I hope this helps someone else out there!

  • Jonah
Jonah