Welcome to WebmasterWorld Guest from 174.129.96.175

Forum Moderators: Ocean10000 & incrediBILL & phranque

www to non www redirect

found non www to www redirect

   
2:10 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



In this thread [webmasterworld.com]I found rules for redirecting non www to www. The Code from jdMorgan is:

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

I want to do the opposite, i.e. www redirect to non www, would the following be correct?

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

If correct, should I put it in my http.conf file or in .htaccess?

mod_rewrite is loaded

Should I create a ServerAlias in my VirtualHost first?

Hope someone has time to help an absolute beginner.

6:09 pm on Jan 18, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Here's the complete library for use in .htaccess:

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

-or-

# Redirect if example.com (case-insensitive) to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

> I want to do the opposite, i.e. redirect www to non-www

# Redirect if NOT example.com (exactly) to example.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]

-or-

# Redirect if www.example.com (case-insensitive) to example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

All differences between this and what you posted are intentional. Select the code snippet that matches your needs. Note that using the "NOT <canonical-domain>" versions precludes the use of subdomains other than "www", but is more comprehensive.

For use in httpd.conf, change the rule pattern from "(.*)" to "^/(.*)$".

If the non-canonical www domain already works --that is, if it returns the same content as the non-www domain-- then no changes to your ServerName/ServerAlias configuration are necessary.

Jim

12:32 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



Thank you Jim, you're a goldmine!
6:02 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



Hi Jim,

You couldn't do me a favour and explain this to me as if I was, well, maybe six? What I really need is a nice, easy piece of text that I can put in an ht file (I did that once before to do 404s). But I don't get all the example.com and ^>%! stuff.

What I'm trying to do is get www.mysite.com redirected to mysite.com. The reason is that Yahoo indexes everything with www, and its splitting my pagerank.

Any chance I can cadge an explanation?

Thanks

Brian

2:55 am on Jan 20, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Either the third or fourth ruleset posted above will redirect www- to non-www.

But there's no such thing as "easy" with mod_rewrite. It's a server configuration tool, and demands a bit of study. Otherwise you're asking me to just give you a "quick run-down" on how to fly a jet fighter off an aircraft carrier in a raging storm, and then land again safely -- ain't gonna happen, and I'd be doing you no favor to try... One typo or logic error and --if you're lucky-- you'll shut down your server immediately. If you're not lucky, the error will be subtle, and will slowly destroy your search engine rankings.

If you're having problem with regular-expressions pattern-matching tokens, then take a look at the many regular expressions tutorials available on the Web. Learning regular expressions will stand you in good stead, as they're used in many scripting languages such as PERL, PHP, and many others.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

9:39 am on Jan 20, 2007 (gmt 0)

10+ Year Member



Thanks Jim
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month