How to 301 redirect url's with .htacces and mod_rewrite?

Discussion in 'Search Engine Optimization' started by Styling Designs, Dec 18, 2009.

  1. #1
    Hi you all, I use Joomla and I have made my url's sef friendly and now I'm getting some 404 errors on some links.
    Now I would like to 301 permanent redirect all my old (long) url's to preserve my Google score and linkjuice.

    I thought it was very simple by adding (for example):
    Redirect 301 /index.php/ http://www.styling-designs.com/

    somewhere in the .htacces file after "RewriteEngine On" but if I do that my site won't show up anymore.

    Can someone tell me what to do I have no clue at this point and my hair is getting grey ;)
     
    Styling Designs, Dec 18, 2009 IP
  2. Canonical

    Canonical Well-Known Member

    Messages:
    2,223
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    110
    #2
    You don't want to redirect ALL old URLs to your home page. You want to redirect each page that's 404'ing on a one-by-one basis to the new URL that renders the same/equivalent content as the old URL did.

    Say for instance you have 3 old URLs that are 404'ing as follows:

    http://www.example.com/index.php?p=123
    http://www.example.com/index.php?p=456
    http://www.example.com/index.php?p=789

    And say the above 3 pages now live at the following URLs:

    http://www.example.com/about-us/
    http://www.example.com/products/sony-bravia-hdtv-40-in/
    http://www.example.com/products/samsung-led-hdtv-50-in/

    respectively.

    In your .htaccess in your root directory adding the folling rules should take care of redirecting requests for the old three old URLs to the three new search engine friendly URLs:


    NOTE: The above assumes that the ONLY query string parameter used on the index.php URLs is p=. Without knowing EXACTLY the URLs that are 404'ing and EXACTLY the new URLs that render the same content as the old URL, it's impossible to give you an EXACT solution. But the above example, may get you started.

    If you'll post the URLs that are 404'ing and where they should be redirected to, I would be glad to provide you with ONE solution to implement the 301 redirects using Mod_Rewrite. There are likely many such solutions.
     
    Canonical, Dec 18, 2009 IP
  3. Styling Designs

    Styling Designs Member

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Sorry for my late reply but I'm really bad at this 301 redirect thing and I want to be absolutely sure that I do it good.

    I found a similar 301 redirect rule on seomoz. Can anybody tell me what the difference is between these 301 redirect rules?

    Here is the 301 rule I got from seomoz website:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
    RewriteRule (.*) http://www.mysite.com/$1 [L,R=301]

    This should redirect hhtp://mysite.com to http://www.mysite.com
     
    Styling Designs, Dec 28, 2009 IP
  4. Canonical

    Canonical Well-Known Member

    Messages:
    2,223
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    110
    #4
    To understand Mod_Rewrite, you first need to understand regular expression (regex) basics. Regex is a language for expressing generic patterns to match on.

    When Mod_Rewrite sees:

    it first evaluates and compares the regex pattern "(.*)" to some portion of the URI. It depends on where the .htaccess is located, but this particular rule is typically placed at the bottom of the .htaccess in the root folder of your web.

    The "." in "(.*)" matches any character... the "*" following the "." indicates to match zero or more occurrences of the previous construct. In otherwords ".*" will match zero or more occurrences of any character - in other words, any string including the empty string. Because it matches zero or more characters ".*" ALWAYS results in a match. The parentheses around the ".*" says whatever matches the pattern inside the parens should be assigned to the reference $1.

    If you request http://mysite.com then $1 will be set to "" (the empty string).
    If you request http://mysite.com/page.html then $1 will be set to "page.html".
    If you request http://mysite.com/folder/page.html then $1 will be set to "folder/page.html"

    NOTE: This is oversimplified as what it sees as the input string to match against depends on the RewriteBase directive and which folder's .htaccess is being evaluated at the time. But anyway... to carry on...

    Anytime the pattern in the RewriteRule matches the input string (URI), the Mod_Rewrite engine then evaluates all of the RewriteConds above it... In this case it evaluates:

    The "%{HTTP_HOST}" is a server variable that contains the hostname portion of the requested URL. In all of the above examples, it would contain the string "mysite.com". The "%{HTTP_HOST}" is the input string that gets compared against the regex pattern "^mysite\.com" to see if the input string (requested host from the HTTP header) matches the pattern.

    The pattern "^mysite\.com" can be broken down as follows... The "^" anchors the pattern that follows to the beginning of the input string. In other words, it's say, the string MUST start with "mysite". Since "." represents any character (a wildcard essentially) but in this case you want to match a literal period ("." character itself), the "." must be escaped in the pattern. So to match the pattern, the input string must begin with "mysite" followed by the literal character "." followed by "com".

    By the way the [NC] stands for no case... or not case sensitive.

    If the input string matchs this pattern (as it would for all 3 example URLs above) then the RewriteCond will evaluate to TRUE or MATCHED. If ALL RewriteConds above a RewriteRule evaluate to TRUE or MATCHED then execution returns to the RewriteRule... where the URL rewriting actually happens. In other words, it executes the following bolded portion of the RewriteRule:

    The Location in the HTTP header is set to "http://www.mysite.com/$1" after whatever matched $1 in the "(.*)" has been substituted. So in the above 3 example URLs, the Location would be set to:

    http://www.mysite.com/
    http://www.mysite.com/page.html
    http://www.mysite.com/folder/page.html

    respectively.

    There are two "flags" at the end of the RewriteRule (i.e. "[L,R=301]). The "R=301" flag tells Mod_Rewrite to set the HTTP Status in the HTTP header to 301. The "L" tells Mod_Rewrite that IF the RewriteRule and all associated RewriteConds evaluated to TRUE or MATCHED and a URL rewrite occurs then it should be the LAST RewriteRule evaluated for this request.

    If either the RewriteRule pattern failed to match the RewriteRule pattern or any of the RewriteConds failed to match their patterns then Mod_Rewrite will stop evaluating that RewriteRule and drop through to the next one.

    Again this is WAAAAAAAAAAY oversimplified. But hopefully, you'll begin to get the picture. I would recommend NOT implementing any Mod_Rewrite rules on your live site until you're comfortable and understand how redirecting web pages with 301 redirects works.
     
    Last edited: Dec 28, 2009
    Canonical, Dec 28, 2009 IP
  5. Styling Designs

    Styling Designs Member

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Thanks for reply but didn't need to look into it soo deep as sh404sef created my sef url's and redirected them automatically. I just learned by trial and error and it worked out fine.

    Happy new year :)
     
    Styling Designs, Dec 30, 2009 IP
  6. amorosso

    amorosso Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    sh404 will lag your site down to no end. You're best to just leave your urls alone. Do mod rewrite via Joomla core. besides make one mistake with sh404 and all your sef urls are gone. Change one setting change and the old ones are gone. I used sh404 for years, so trust me not worth it. The lag is big time.
     
    amorosso, Jan 17, 2010 IP
  7. Styling Designs

    Styling Designs Member

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    I know what you mean Amorosso I have experienced this already.
    Can you tell me how you do the mod rewrite with the Joomla core?
    I know that the rewrite rule can be different for the server of my hosting company but if I can get a hint I can experiment and make it work by trial and error.

    Best wishes,

    Wouter
     
    Styling Designs, Jan 18, 2010 IP
  8. amorosso

    amorosso Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    After a very long hard lesson and the lack of support of the guys from sh404 I learned and read many times to just use joomla core mod. in global settings to mod rewrite, and of course change your htaccess.txt to .htaccess I also learned that Joomla it self is a joy killer in resources. I switched to a much much better CMS that I found on elfsoup.com I couldn't be happier. I mean I still turn my customers onto Joomla for the price, free, LOL. Go check out elfsoup.com you'll be happy that you did.
     
    amorosso, Jan 18, 2010 IP
  9. verdecove

    verdecove Member

    Messages:
    287
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #9
    I can not agree with you. I'm using sh404 for several months now and purge sef url's on a regular basis after I added , deleted or moved products into another category of my shop and the sef url's are always just fine. You only will run in big time trouble if you tried to add meta tags or meta descriptions manually. That really screws up absolutely everything and it takes a very very long time to fix that. So do not add any meta tags or descriptions manually to sh404!
    The only problem that occurs sometimes on my site, and the developer is working on that bug and a new version of sh404 is to be released soon, sometimes the user can not return to page one if there are several product pages in a category.
    I admit, this is annoying and a problem because it can cause lost sales, but they are working on testing the new version and the bug will be fixed soon as per Dev Anything Digital.
     
    verdecove, Jan 18, 2010 IP
  10. amorosso

    amorosso Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I hear ya. I just it my self for a few years. I thought it was the best thing since sliced bread. But the resources used are nuts. I had a very busy Joomla site with over 1000 users per day with over 6k pages. I switched to a different CMS from elfsoup.com and wow I love it. My users love the speed. I mean don't get me wrong Joomla and sh404 is ok, but for a more busier site I'll stay away from at least sh404.

    The CMS I use now, all urls are sef and all meta tags are created automatically in seconds.

    sometime I use to find post from unhappy Joomla users stating all that I have been saying. And I was always like they are wrong.. Not anymore.. But if I'm not mistaken didn't sh404 go for money now..
     
    amorosso, Jan 18, 2010 IP
  11. Styling Designs

    Styling Designs Member

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #11
    I'm looking on elfsoup.com but I can't find out which cms you are using, could you tell us?
    It seems like a good site with resonable priced subscriptions.
    Did the domain transfer to elfsoup when smooth for you?

    And yes sh404sef is now commercial, I think about $19,- so that is not a very big problem.


    And about my sh404sef problem: After installing sh404sef my sef url's worked just fine and I was very happy.

    I'm not sure what messed up my sef url's but probably I pressed one time to many the clear cache button after changing settings in the configuration > meta/seo tab which I probably shouldn't have done.
    Maybe someone knows more about this?
     
    Styling Designs, Jan 18, 2010 IP
  12. verdecove

    verdecove Member

    Messages:
    287
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #12
    I will give the CMS from elfsoup a "looksie".
    The reason why I went with Joomla is Virtuemart. So far I don't have a traffic that I would have to worry about resources and I know people using Joomla and VM and getting traffic of 1000 and more UV's a day without having any problems.
    Yes, sh404 is commercial for several months now. Cost $35.00 which pissed me off quite a bit after I found out about the pagination problem it had.
    Still I think it's a pretty good tool although more and more CMS and free carts coming out the provide the pretty much the same features for free.
     
    verdecove, Jan 18, 2010 IP
  13. amorosso

    amorosso Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Yes it went very smooth. I have become somewhat friends I would say with over the time. They are always out to help you.
     
    amorosso, Jan 18, 2010 IP
  14. D3rek

    D3rek Well-Known Member

    Messages:
    1,345
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    100
    #14
    D3rek, Jan 19, 2010 IP
  15. 1travel1

    1travel1 Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    1travel1, Jan 19, 2010 IP
  16. midmed

    midmed Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    I cannot get rid of the variables in the new URL. For example, user=blahblah&goto=profile, gets attached to the redirected url. I am trying to get around some problems with google. Is there a way to trim the variables?

    Redirect: Index.php?goto=Profile&user=AnyPossibleUser
    to: Index.php without anything trailing?
     
    midmed, Jun 14, 2012 IP
  17. same2cool

    same2cool Greenhorn

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #17
    it is easy to take access into linux server, window server. but in godaddy web designer it is impossible, because it does not provide this feature. I have a site, without this feature. ADHD testing jacksonville Florida
     
    same2cool, Jun 14, 2012 IP