Forum Moderators: phranque
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.
# 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]
# 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]
# 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]
# 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]
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
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
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