homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
www to non www redirect
found non www to www redirect
ulstrup




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

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.

 

jdMorgan




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

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

ulstrup




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

Thank you Jim, you're a goldmine!

Brian




msg:3225154
 6:02 pm on Jan 19, 2007 (gmt 0)

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

jdMorgan




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

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

Brian




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

Thanks Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved