Urgent! How to write the 301 redirect code.

15 replies
Google is treating site address with and without www as two different site. So I'm trying to figure out how to do a 301 redirect from www to non-www address. Can someone help me out. I searched over the internet. I keep running into errors.

What should I put into the code for 301 redirect www.solarpanelselectricity.us to solarpanelselectricity.us?
#301 #code #redirect #urgent #write
  • Profile picture of the author aaron_nimocks
    Change .htaccess to

    PHP Code:
    RewriteEngine on
    RewriteBase 
    /

    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule ^(.*) http://www.example.com/$1 [R=301] 
    Does this work?
    Signature

    My free PSD logs can be downloaded at PSD Bum. Enjoy!

    {{ DiscussionBoard.errors[3106194].message }}
    • Profile picture of the author BinBinWu
      I think this is redirect from non-www. to www. I wanna the opposite.
      {{ DiscussionBoard.errors[3106199].message }}
      • Profile picture of the author aaron_nimocks
        Originally Posted by BinBinWu View Post

        I think this is redirect from non-www. to www. I wanna the opposite.
        Did you try it? What did it do?
        Signature

        My free PSD logs can be downloaded at PSD Bum. Enjoy!

        {{ DiscussionBoard.errors[3106204].message }}
  • Profile picture of the author BinBinWu
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^solarpanelselectricity.us
    RewriteRule (.*) http://www.solarpanelselectricity.us/$1 [R=301,L]

    I put directly under the htaccess file under my root folder right? I cant access my website after this .
    {{ DiscussionBoard.errors[3106205].message }}
    • Profile picture of the author aaron_nimocks
      Try this

      PHP Code:
      RewriteEngine on
      RewriteBase 
      /

      RewriteCond %{HTTP_HOST} ^solarpanelselectricity.us$
      RewriteRule ^(.*) http://www.solarpanelselectricity.us/$1 [R=301] 
      If this don't work then your server might not be setup for rewriting
      Signature

      My free PSD logs can be downloaded at PSD Bum. Enjoy!

      {{ DiscussionBoard.errors[3106212].message }}
  • Profile picture of the author BinBinWu
    Internal Server Error
    {{ DiscussionBoard.errors[3106213].message }}
  • Profile picture of the author Steven Fullman
    Depending on your web host, you could add this code to your .htaccess file:

    Code:
    RewriteEngine On
    RewriteBase /
    RewriteCond % ^www.solarpanelselectricity.us [NC]
    RewriteRule ^(.*)$ http://solarpanelselectricity.us/$1 [L,R=301]
    Who is your web host?

    Steve

    EDIT: WOW: Lots of activity already!
    Signature

    Not promoting right now

    {{ DiscussionBoard.errors[3106214].message }}
  • Profile picture of the author BinBinWu
    bluehost is my server lol. I think you guys code is right. I'm pretty sure, i did right tho
    {{ DiscussionBoard.errors[3106221].message }}
  • Profile picture of the author webtrading
    Several years ago I made at the time a difficult decision to stop using www with all my websites and have never looked back. No www. has advantages, including a cleaner and shorter look to it.

    Don't understand how it could be a server issue since I believe all servers work with .htaccess and don't see why you would need some of the misc code referenced above.

    Even though I am not a programmer/coder I still know for sure the code below (in your .htaccess file in the root directory) is valid as I have been using it successfully for years with all my sites. Be sure to copy and paste it exactly (making sure you name it .htaccess with the dot in front, without the dot it won't work), only change the domain name.

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.example\.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
    {{ DiscussionBoard.errors[3106367].message }}
    • Profile picture of the author Steven Fullman
      Originally Posted by webtrading View Post

      don't see why you would need the 'rewritebase /' line of code referenced above.
      It's to make sure the correct URL base is used, since most web addresses aren't related to physical filename paths.



      Cheers,
      Steve
      Signature

      Not promoting right now

      {{ DiscussionBoard.errors[3106445].message }}
      • Profile picture of the author webtrading
        So if I add a rewritebase / line would it improve my coding and can you give an example of its use since don't understand how it would come into play. Thanks

        P.S. I already use this in the index file meta tags area: <base href="http://Example.com/">



        Originally Posted by Steven Fullman View Post

        It's to make sure the correct URL base is used, since most web addresses aren't related to physical filename paths.



        Cheers,
        Steve
        {{ DiscussionBoard.errors[3106539].message }}
        • Profile picture of the author Steven Fullman
          Originally Posted by webtrading View Post

          So if I add a rewritebase / line would it improve my coding and can you give an example of its use since don't understand how it would come into play. Thanks

          P.S. I already use this in the index file meta tags area: <base href="http://Example.com/">
          Hi

          It's more to do with Linux filesystems/directory aliases than coding.

          I use rewritebase / out of habit, to be honest.

          Steve
          Signature

          Not promoting right now

          {{ DiscussionBoard.errors[3106614].message }}
  • Profile picture of the author BinBinWu
    Ok. I fixed the issure. Scare the hell out of me. Here is how I did it. Maybe useful for anyone else might run into the same kind of trouble.

    1. Wiped out my entire htaccess file. Then use the code you guys give me to redirect

    2. After that, only thing that works is my homepage and admin chanels. All posts and pages went down

    3. I login to my wordpress Admin page, updated all my post and pages and plugins. Then all comes to normal. And the redirect works too lol
    {{ DiscussionBoard.errors[3106443].message }}
  • {{ DiscussionBoard.errors[3106487].message }}
  • Profile picture of the author I.M.Retired
    Blue Host has a tutorial on this topic at:

    BlueHost Tutorials :: Redirecting Web Pages

    If you are using Wordpress for your site, you may get an error message, as Wordpress has a built-in automatic redirect that may cause a conflict.
    {{ DiscussionBoard.errors[3106636].message }}

Trending Topics