301 Redirects (.htaccess)

8 replies
Hi Guys,

I am trying to on on 301 Redirects for a store and Google Webmaster Tools keep indicating a big list of 404 pages. I made adjustments from 100 or so old urls giving 404's and some of the links work and some of them don't.

The 301 Redirects are the right format which I currently set up as:

Code:
Redirect 301 /catering-food-thermometers/c2/36 http://www.tomsgadgets.com/thermometers/food-thermometers.html
Now the problem seems to occur (I guess) is when URL'S have special characters such as the *,?,=,& etc so if this is the case, what kind of rule do I need to implement for these Redirects to work, as Google crawled the site, it picked up over 1,000 more errors (The URL's for the old website was horrible so I am having to redirect so many URL's).


Example of a Rule I am thinking might work:

Code:
RewriteRule ^tag/(.*)\=(.*)$ http://www.tomsgadgets.com/ [R=301,L]
RewriteRule ^tag/(.*)\?(.*)$ http://www.tomsgadgets.com/ [R=301,L]
RewriteRule ^tag/(.*)\&(.*)$ http://www.tomsgadgets.com/ [R=301,L]
RewriteRule ^tag/(.*)\%(.*)$ http://www.tomsgadgets.com/ [R=301,L]
Would this work? Or is there something else I need to implement for these 301 redirects to read special characters?

PS: The platform the store is built on is Magento.


Highly Appreciate it.
Steve.
#301 #htaccess #redirects
  • Profile picture of the author cgimaster
    Yes it should work however why all those lines instead of a single one like this:
    Code:
    RewriteRule ^tag/(.*) http://www.tomsgadgets.com/ [R=301,L]
    Also to use it like that, make sure you have above it, the following defined:

    Code:
    RewriteBase /
    Or it may not work as expected in some cases.
    {{ DiscussionBoard.errors[7293404].message }}
  • Profile picture of the author Shaolinsteve
    Thank you for your reply.

    The reason I created those lines was because I thought I would have to define each special character with a new rule. I have scouted elsewhere and also seen people using $ to close those rules? Is that also necessary?

    Thanks for the advice, I really appreciate it!
    {{ DiscussionBoard.errors[7293434].message }}
    • Profile picture of the author cgimaster
      Originally Posted by Shaolinsteve View Post

      Thank you for your reply.

      The reason I created those lines was because I thought I would have to define each special character with a new rule. I have scouted elsewhere and also seen people using $ to close those rules? Is that also necessary?

      Thanks for the advice, I really appreciate it!
      $ is to determinate that a rules will end with in a specifict way which you does not have to determinate in your case as you dont know it.

      For example I know the url starts with A and ends with B:

      Code:
      /A/whateverinbetween/B
      So I could use the following:

      Code:
      ^/A/(.*)/B$
      The (.*) will take care of everything after tag/ so you should be ok with just that.

      For instance the following rule:

      Code:
      ^tag/(.*)
      Should match:

      mydomain.com/tag/whatever
      mydomain.com/tag/whatever?id=1
      ...
      mydomain.com/tag/whatever?id=1&name=3
      etc

      Here is a working test sample for you to use and play with:
      http://rubular.com/r/RqvXNFcQef

      Its very usefull but slight different from .htaccess rules in some things but should be enough to show you how it will work

      In regards the regex mod_rewrite uses you can read more at:
      http://httpd.apache.org/docs/current/rewrite/intro.html

      Code:
      Character     Meaning     Example
      .    Matches any single character    c.t will match cat, cot, cut, etc.
      +    Repeats the previous match one or more times    a+ matches a, aa, aaa, etc
      *    Repeats the previous match zero or more times.    a* matches all the same things a+ matches, but will also match an empty string.
      ?    Makes the match optional.    colou?r will match color and colour.
      ^    Called an anchor, matches the beginning of the string    ^a matches a string that begins with a
      $    The other anchor, this matches the end of the string.    a$ matches a string that ends with a.
      ( )    Groups several characters into a single unit, and captures a match for use in a backreference.    (ab)+ matches ababab - that is, the + applies to the group. For more on backreferences see below.
      [ ]    A character class - matches one of the characters    c[uoa]t matches cut, cot or cat.
      [^ ]    Negative character class - matches any character not specified    c[^/]t matches cat or c=t but not c/t
      {{ DiscussionBoard.errors[7293471].message }}
  • Profile picture of the author Shaolinsteve
    Thank you.

    I have just given this a go using the following:

    Code:
    ############################################
      ## enable rewrites
    
      Options +FollowSymLinks
      RewriteEngine on
      RewriteBase /
      RewriteRule ^tag/(.*) http://www.tomsgadgets.com/ [R=301,L]
    
      ############################################
      ## you can put here your magento root folder
      ## path relative to web root

    Now before using the code all the other 301 redirects not using special characters work fine. But all URL'S using characters like %,*,= do not work. Such as:

    Code:
    Redirect 301 /products/details.aspx?pc=SER033-10 http://www.tomsgadgets.com/

    The rubular website you provided also showed the code like this: ^tag\/(.*) so I placed a closed dash in front of tag and the URL's are still not working and remaining as a 404 page.

    I really appreciate the advice so far.
    {{ DiscussionBoard.errors[7293624].message }}
  • Profile picture of the author cgimaster
    Here is an interesting tool for testing the rules:

    http://martinmelin.se/rewrite-rule-tester/

    After http://hostname/ put:
    Code:
    products/details.aspx?pc=SER033-10
    On the rules add:
    Code:
    RewriteEngine on
    RewriteBase /
    RewriteRule ^products/(.*) http://www.tomsgadgets.com/ [R=301,L]
    Then click "RUN TEST" the rule pass just fine.

    Well the * is like a wildcard that will match everything if that is not working for you, you could try this:

    RewriteRule ^tag/([a-zA-Z -_\?\&\=\$\.\/]+) http://www.tomsgadgets.com/ [R=301,L]

    Basically you are defining the entire expression of what you want to allow after tag/

    As for the \ before the / its not need on the mod_rewrite, that is one of the differences from the normal regex including the rubular site.

    Also try using ^/tag/ instead of just ^tag/ on the previous sample just to make sure that was not the issue.

    You can always check the rewrite_log (if you dont have one it may be possible it is saved altogether in the error_log) file on your ftp it usually tells you if any error occurred within the rewrite rules.
    {{ DiscussionBoard.errors[7293781].message }}
  • Profile picture of the author Shaolinsteve
    I have to come back to this...

    2 Problems are now occuring. I have managed to fix the /products URL's from your help. I am now confused as to how these 301 redirects redirect to the right URL.

    Let's take a few URL's as an example:

    Code:
    Redirect 301 /ft/ http://www.tomsgadgets.com/
    Redirect 301 /ft http://www.tomsgadgets.com/
    Redirect 301 /lx-desk-moun.. http://www.tomsgadgets.com/
    Using the tool you provided are showing as a failed rewrite. I'm confused myself as to how some of the URL's were even made in the first place. I changed the products/ into the tag and it still cannot rectify it so I am stuck as to how to get this solved.


    Last problem is to me it looks as if the rule redirects them to the home page (Ignore the code above, they are supposed to go to home) but we moved from 1 platform to another so a lot of these URL's are being directed to the exact product.

    Does this rule direct these links to...

    Code:
    /products/details.aspx?pc=THR020-43&url=digital-cooking-thermometer http://www.tomsgadgets.com/digital-cooking-thermometer.html
    The new URL or does the rule overwrite that and just direct it to the homepage?


    Highly appreciate your thoughts and anyone elses on this.

    Steve.
    {{ DiscussionBoard.errors[7314396].message }}
    • Profile picture of the author cgimaster
      Hi Steve,

      Could you PM me your currently .htaccess being used as it is, I will take a look and get back to you asap.
      {{ DiscussionBoard.errors[7315851].message }}
  • {{ DiscussionBoard.errors[7327730].message }}

Trending Topics