301 Redirect from non-WWW to WWW Problem in WP

Discussion in 'Apache' started by jhmattern, Mar 22, 2007.

  1. #1
    I'm trying to direct BizAmmo.com to www.BizAmmo.com using the following format:

    RewriteEngine On
    RewriteEngine onRewriteCond %{HTTP_HOST} ^bizammo\.com
    RewriteRule (.*) http://www.bizammo.com/$1 [R=301,L]
    Code (markup):
    The site is run with Wordpress as the backend.

    It works in the sense of redirecting like I mentioned, but when I do that, it screws up the category links, and makes every category link to the home page rather than the category page.

    Is there anything I can do about it, or should I just forget about the redirect?

    Thanks! :)

    Jenn
     
    jhmattern, Mar 22, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    That should work - you could try adding start/end symbols to the regex pattern to ensure the whole URL gets captured:
    RewriteRule ^(.*)$ http://www.bizammo.com/$1 [R=301,L]

    Or you could try using the REQUEST_URI variable:
    RewriteRule .* http://www.bizammo.com%{REQUEST_URI} [R=301,L]
     
    rodney88, Mar 22, 2007 IP
    jhmattern likes this.
  3. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #3
    No luck in either case unfortunately. It still has the category links point to the home page. :( Thanks though. :)
     
    jhmattern, Mar 22, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ah, right. Sorry I misunderstood. The redirection is fine but it's the category links? The rewrite rules shouldn't affect the actual URLs that WordPress renders so I'm not sure where the problem is - is it the target href in the rendered links (i.e. location shown in status bar when you hover over them) that is wrong or is just when you click it you get redirected to the wrong place? If it's the latter, it would help if you posted the whole .htaccess file.
     
    rodney88, Mar 22, 2007 IP
  5. Crooked Pitch

    Crooked Pitch Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey Rodney88, I just ran into the same exact problem that jhmattern ran into, which is what drew me to this thread. In my situation, the latter case is the issue. When I hover over my Wordpress category link, it shows the proper url. But when I click on it it just goes to my main page.

    I'm willing to post my .htaccess file, if you're still willing to assist on this issue???

    Also, could this problem be related to the fact that I just instituted my 301 redirect? Is there some time period that these changes take to become effective?

    Thanks,
    Knox
     
    Crooked Pitch, Apr 23, 2007 IP
  6. Crooked Pitch

    Crooked Pitch Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Here's my .htaccess file. Thanks, in advance, for your help.

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

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

    # END WordPress
     
    Crooked Pitch, Apr 23, 2007 IP
  7. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #7
    rodney... not sure how I missed your last response, so I apologize.

    I did eventually get a 301 redirect working to direct non-www to www pages on my site without a redirect problem. However, the 301 redirect caused all other crazy problems on every blog I implemented it on (it created an odd login loop / problem, it disabled all commenting on my blogs, and it screwed up the wysiwyg editor on at least one of the blogs, which made my post content disappear on publishing). I can't explain it, and it happened with multiple versions of the redirect that were given to me by programmers and that I found through tutorial sites, so I had to disable it completely unfortunately. :( But removing it solved every other problem.
     
    jhmattern, Apr 23, 2007 IP
  8. Monty

    Monty Peon

    Messages:
    1,363
    Likes Received:
    132
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Did you try with this code ? :

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
    RewriteRule ^(.*)$ http://www.yousite.com/$1 [L,R=301]
    Code (markup):
    It should only do a proper redirect, with no side effects.

    ps: glad to see you solved those weird problems with yours blogs.
     
    Monty, Apr 23, 2007 IP
  9. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #9
    It looks exactly like what I was using before. Just to try though, I just plugged it into one of my htaccess files for a blog, and as soon as I added it, the blog stopped acknowledging my username and password again. It's definitely a redirect problem. :(
     
    jhmattern, Apr 23, 2007 IP
  10. Monty

    Monty Peon

    Messages:
    1,363
    Likes Received:
    132
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Maybe a last try with this WP plugins ( I just remember it now :eek: ) :

    Enforce www. Preference


    It's widely used, hope it will work for you.
     
    Monty, Apr 23, 2007 IP
    jhmattern likes this.
  11. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #11
    I'll give that a try later today maybe, and report back if it gives me trouble too. Thanks. :)
     
    jhmattern, Apr 23, 2007 IP
  12. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #12
    jhmattern, it sounds like WP is not generating the correct canonical hostname as the URL for the form action. When you submit the data to WP to process it, it's intercepted by your mod_rewrite www/non-www rule and forces a redirect instead, giving the impression of nothing happening at all. I couldn't say for certain but can't think of any other explanation - in this case you would be best off using the plugin posted above, as it sounds like it will force WP to generate the correct URL every time.

    If you want to stick with a mod_rewrite solution, you could try adding in another condition that does not do the redirect if we are posting data. Eg:
    RewriteEngine On
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^bizammo\.com$
    RewriteCond %{REQUEST_METHOD} !^POST$ [NC]
    RewriteRule ^(.*)$ http://www.bizammo.com/$1 [R=301,L]
    Code (markup):
    Crooked Pitch, you need to force www before WordPress can intercept and rewrite to the index script that powers the blog. Eg:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # Force www
    RewriteCond %{HTTP_HOST} ^crookedpitch.com [NC]
    RewriteRule ^(.*)$ http://www.crookedpitch.com/$1 [L,R=301]
    
    # WordPress rewrite
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    Code (markup):
     
    rodney88, Apr 23, 2007 IP
    jhmattern likes this.
  13. Crooked Pitch

    Crooked Pitch Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Rodney and Monty... Thank you so much for your assistance on this issue.

    I installed the WP plug in and everything works great. I installed the plugin before Rodney posted his last entry. Quick question...

    For SEO purposes only, am I better off staying with the plugin or trying Rodney's last recommendation?

    Thanks again,
    Knox
     
    Crooked Pitch, Apr 23, 2007 IP
  14. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #14
    Question about the plugin... would something like that interfere with rewrite rules about permalink formatting?
     
    jhmattern, Apr 23, 2007 IP
  15. Monty

    Monty Peon

    Messages:
    1,363
    Likes Received:
    132
    Best Answers:
    0
    Trophy Points:
    0
    #15
    No, it should respect your settings. If you personalized your URL structure, it will keep the choosen permalink format.
     
    Monty, Apr 23, 2007 IP
  16. YoungMaster

    YoungMaster Peon

    Messages:
    330
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #16
    I had the same problem with performing a 301 to the www version with a wordpress blog.

    The .htaccess looked like this originally.

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    Code (markup):
    Then I tried changing it to this.

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
    RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
    Code (markup):
    After I did that all links on my site redirected to the index.php page.

    I played around with it for awhile and finally found that it worked if I simply placed the redirect first, like this...


    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
    RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
    
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    
    Code (markup):
    I don't know why that fixed it, but it worked. Perhaps someone with more knowledge of .htaccess could come along and give an explanation.

    Anyways. Hope that helps.
     
    YoungMaster, Apr 30, 2007 IP
  17. jhmattern

    jhmattern Illustrious Member

    Messages:
    8,909
    Likes Received:
    794
    Best Answers:
    2
    Trophy Points:
    455
    #17
    Unfortunately, mine was originally first, and all of the problems still occurred, so that wasn't the problem in my case. But thanks for reminding me about the issue. I forgot to try that plugin, so I'll be sure to do that today. :)
     
    jhmattern, Apr 30, 2007 IP
  18. YoungMaster

    YoungMaster Peon

    Messages:
    330
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #18
    No problem. To bad it did not work.

    Anyone else out there with some insight into this? I would love to really figure this one out. Seeing as this is a common Seo technique applied when using wordpress.

    Guess I am off to go do some reading on the subject. Does anyone know some good resources for learning about .htaccess? Either good forum threads or in depth articles.
     
    YoungMaster, Apr 30, 2007 IP
  19. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #19
    WordPress can generate URLs in whatever format it wants to. It then uses mod_rewrite to rewrite every request to the single index.php script, which interprets the requested URI and displays the content accordingly.

    If you add in a redirect after the WordPress rules, the request would have already been rewritten to the index.php script. Normally it stops here and you'd never know about the rewrite, but when a redirect follows it sends out a moved header and your browser requests the new location. As the redirect only forces in the www subdomain, you will always get sent to index.php

    If you add in a redirect before the WordPress rules, the request has not already been rewritten to index.php so you the redirect will behave as expected.
     
    rodney88, Apr 30, 2007 IP
    YoungMaster likes this.
  20. YoungMaster

    YoungMaster Peon

    Messages:
    330
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Thank you. Just what I was looking for. Rep given.
     
    YoungMaster, Apr 30, 2007 IP