Welcome to WebmasterWorld Guest from 54.157.222.62

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Permanent Redirect

   
4:47 pm on Jan 31, 2012 (gmt 0)



Hello all,

I am in the process of streamlining our website and am concerned about the fact that we are using BOTH www and non-www. I am going to use a 301 redirect, but I am concerned about losing functionality of the site due to numerous plugins etc that are hard coded to use WWW. So, I suppose my question is, if I use this code that I edited from several sources:


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ www.%1.%2%{REQUEST_URI} [R=301,L]


Will this solve our duplication issues sitewide? For instance. if someone visited:

http://example.com/dir1/dir2/dir3/product.php

Would they effectively be led to

http://www.example.com/dir1/dir2/dir3/product.php ?

Basically I am wanting to know if this will work for sitewide redirects versus just redirecting the landing page.

Thanks!

Zak
9:46 pm on Jan 31, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Urk. Before anything else, get your dots . sorted out. In Regular Expressions, . means "any one character". If you mean a literal period, like the one in "dot com", you have to say \.

RewriteRules apply to all requests, not just the ones that your user typed in from "outside". So that includes any and all include files and similar.

All your Conditions can be merged into one.

:: fingers crossed, because last time I did this I garbled the punctuation ::

RewriteCond %{HTTP_HOST} !(^www\.example\.com$)?
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

That is: if the host is anything other than exactly www.example.com or exactly nothing, redirect to...

I don't know where the %1 and %2 in your Rule are coming from. The only thing you need to capture and reuse is the request.

The form ^.*$ is always redundant. By default, Regular Expressions start as soon as they can and go on as long as they can. You only need anchors when you are looking for specific text, or capturing text in a specific location.
9:53 pm on Jan 31, 2012 (gmt 0)



Thank you for your explanation! This is probably the best explanation I've found on the web ... My understanding to the single "." was wrong! I have used your example, and the redirect is working flawlessly .. I have tested the Headers and they are behaving exactly how I was hoping. Thanks Again!

-- Zak
10:06 pm on Jan 31, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



because last time I did this I garbled the punctuation ::
RewriteCond %{HTTP_HOST} !(^www\.example\.com$)?

That's garbled, but in a way that it would appear to work for some requests.

Use:
!^(www\.example\.com)?$
10:32 pm on Jan 31, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



That is: if the host is anything other than exactly www.example.com or exactly nothing


I noticed this in the OP, but why do you need to check that the HTTP_HOST is not empty?
11:04 pm on Jan 31, 2012 (gmt 0)



g1smd,

I did change that ... Because it appeared to work, could you explain the difference between (^...$)? and ^(...)?$ ?
2:45 am on Feb 1, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



why do you need to check that the HTTP_HOST is not empty?

You should have nipped in before g1 got here, because I'm quoting him. It's something about HTTP 1.0 not sending a hostname, so the {HTTP_HOST} might come through as completely empty.

OK, let's see how many different ways I can mangle the punctuation before someone's server melts down and I am banned from the Forum.

Right version:
!^(www\.example\.com)?$
Wrong version:
!(^www\.example\.com$)?

The leading ! is pretty hard to mangle; it's the part that means "is not".

^{blahblah}$ means "the whole text, from beginning to end"
? means "the immediately preceding character or group (either in brackets or parentheses) may or may not be present"

By putting the anchors inside the parentheses, the Wrong Version permits your HTTP_HOST to be... well, just about anything really. Or, more to the point, it permits it to not be just-about-anything.

When testing, don't just feed in things that are supposed to work. Also feed in things that are supposed to not work, including some perfect garbage.

And then wait a few minutes and check your error logs to make sure they're not suddenly ballooning out of all proportion and/or carrying the same timestamp as your regular logs. Been there. Done that.