Forum Moderators: phranque

Message Too Old, No Replies

Redirect or Rewrite non-www ?

redirects vs rewrites

         

bookbuyer

4:06 am on Aug 28, 2011 (gmt 0)

10+ Year Member



I need my site to only begin with "www." Like this:
www.example.com

Currently, I can type anyting in the "www" part. For example:
wwwhello.example.com
abcdefghijk123.example.com
blahblahblah.example.com

Regardless of what prefix I type in, my homepage will be displayed, and the URL in my browser window will display the nonsense prefex, like this: "abcdefghijk123.example.com."

Somehow, the search engines actually indexed one of my pages as abcdefghijk123.example.com.

Doing a normal mod_rewrite doesn't help, because the URL in the browser still shows the incorrect prefix (and the home page is displayed, anyway).

Does that mean I need to use an actual "redirect?" How do I do that and make it search engine friendly? Would someone post the code for a 301 redirect if that it what it needs?

g1smd

4:14 am on Aug 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use mod_rewrite but include both the domain name in the target and the [R=301,L] flags.

Make sure the code runs site wide, not just for root URL. Exact code is in very many previous threads.

bookbuyer

4:27 am on Aug 28, 2011 (gmt 0)

10+ Year Member



Thank you for your very quick reply. Unfortunately, I have found the same code repeated hundreds of times (I've spent over a week researching this), but all that code does is redirect to my home page, with the URL in the browser still showing the same "abcdef123.example.com." So that is why I figured I needed a different code with the word "redirect" in it, so that the URL would actually change? Or is that bad for the search engines? I don't know. But if it would be a good thing, would you please post code that would work?

lucy24

5:39 am on Aug 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you describe is the key difference between a rewrite and a redirect from the user's point of view. (There are other differences behind the scenes.) To create a redirect using mod_rewrite, you just need to give the full url starting in http:// on the "target" side, and append the flag [R=301,L].

Is there an echo in here?

But you may have a different problem. Have you had a talk with your host? It's common to have automatic redirecting (not rewriting) to a with-www or without-www format. But here they're sending completely imaginary, nonexistent subdomains to your front page-- and that really shouldn't be happening.* At least not without your express consent.


* I detoured for a quick check of my own site to make sure this wasn't a standard practice that I'd simply never thought to try. Nope, I get an error message-- not my site's own 404, but the browser itself saying it can't find the server.

g1smd

7:42 am on Aug 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What code did you try?

Hint: If it does NOT include RewriteCond and %{HTTP_HOST} and RewriteRule and http://www.example.com/$1 and [R=301,L] somewhere, then it is the wrong code.

What do you mean by the question "Redirect or Rewrite?"

You should use an external redirect, not an internal rewrite.

You should use a RewriteRule with the [R=301,L] flags. You should not use Redirect or RedirectMatch rules.

bookbuyer

7:50 pm on Aug 28, 2011 (gmt 0)

10+ Year Member



Here is the exact code I've used:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]

I should have articulated that my goal is as follows:
When a visitor (or search engine) goes to "http://abcdef12345.example.com" my server should say "Hey, that's wrong, go here: 'http://www.example.com'"

I felt that is a "reDIRECT" and not a "reWRITE" because it's my understanding that a rewrite "quietly" sends the visitor to the correct "www" page, but doesn't change anything in the URL window in the browser.

Of course, my goal is to do what's right for the search engines. I don't want the search engines thinking that www.example.com and abcdefg123.example.com are the same content, e.g. duplicate content penalty, etc., and I guess as far as real people/real visitors go, I really don't care, it's just so that the SE's don't think I have dup content. The search engine actually indexed one of my pages as abcdef123.example.com and displays it as a snippit :-)

g1smd

8:23 pm on Aug 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, you need a RewriteRule configured to send a 301 redirect, not silently perform an internal rewrite (it can do both, and the syntax is only slightly different for each operation).

Your example code will only fix requests beginning "example.com".

You'll need a different RegEx pattern, "exactly" this:
^(www\.example\.com)?$

and WITHOUT the [NC] flag.

bookbuyer

8:57 pm on Aug 28, 2011 (gmt 0)

10+ Year Member



Uh oh, this one doesn't work at all -- "internal server error too many redirects."

This is the exact code I used:
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]

It's so weird -- the code that I'm currently using to ADD the "www" (if someone completely leaves it off) works perfectly:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]

If you just type in "http://example.com" in to the browser URL window, the server makes the "www" appear in my browser window just fine.

You'd think making "www" appear when visitor types in "abcde1234" would be just as simple, but apparantly it is not :-)

Any ideas?

lucy24

9:05 pm on Aug 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your first rule says "if the host is either exactly www.example.com or exactly nothing, redirect to www.example.com". You can see where the problem is. You need a rule that says (in English) "if the host ends in example.com but is not exactly www.example.com, then redirect to www.example.com".

Always remember that [L] doesn't mean "get out of this htaccess file forever". It just means "stop here, go back to the beginning and repeat until everything rinses clean".

bookbuyer

9:57 pm on Aug 28, 2011 (gmt 0)

10+ Year Member



Okay, so I deleted all the code and made a brand new .htaccess file that only says:

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

... and it does not work!

So, if there is someone out there who knows what code to try, I would sure appreciate it being posted here.

g1smd

10:26 pm on Aug 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's a simple typo. Before the ^ add a ! there.

Leaving that ! off is my most common typo.

The ! means NOT.

Redirect if what was asked for was NOT "exactly" www.example.com is what the rule should do.

bookbuyer

3:58 am on Aug 29, 2011 (gmt 0)

10+ Year Member



So sorry to say that didn't help either. Oh well. I'll consider this matter closed. Thank you all for your helpfulness.

g1smd

7:47 am on Aug 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Post the exact code you are using.

This is a simple two line directive that already works on millions of other sites.

bookbuyer

2:20 pm on Aug 29, 2011 (gmt 0)

10+ Year Member



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

(and I do know to replace the word "example" with the name of my own website :-)

lucy24

3:48 pm on Aug 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Heh. He meant, keep the opening anchor, but add the ! The combination of ! and ? can become unpredictable if it isn't anchored at both ends. You need to be saying (in English) "the host is neither exactly www.example.com nor exactly nothing".

g1smd

5:32 pm on Aug 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes,
!^(www\.example\.com)?$
is what I should have originally typed.

bookbuyer

8:02 pm on Aug 29, 2011 (gmt 0)

10+ Year Member



Okay, it appears that every single little punctuation mark makes or breaks the code and has lots of importance, would someone here (or everyone here)please look at the following 2 lines of code, punctuation-mark by punctuation-mark and see if this is 100% correct:

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

Thank you.

g1smd

8:25 pm on Aug 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It appears that every single little punctuation mark makes or breaks the code
Yes. This is server configuration code. A single misplaced full stop could put your site offline and out of business.

if this is 100% correct
It is if you want to 301 redirect all other domains and all other sub-domains to www.example.com re-appending the originally requested path and query string data.