Www to non www
-
Hi,
Is this the correct code for redirecting www to non www version on Apache server
RewriteEngine
OnRewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Thanks
-
Many Thanks to all of you for your clarifications .
-
Double post... I only clicked one though so... random!
Also, yukky windows servers hehe
-
Hehe I was going non www to www wasn't I, I should learn to read properly, well spotted!
-
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Put that in your .htaccess within your root web directory (typically public_html), and you should be good.
Make sure you save your .htaccess file properly (save as "All files" and not "as text file" so the .txt doesn't get appended to it.
-
The $1 basically copies everything that precedes it ( http://www.example.com/ )
This is so that for each URL it rewrites you don't have to give it the above URL over and over again.
Hope this makes sense
**'**If the matched regex contains groups in parentheses, $1..$9 in the replacement refer to the captured text in the
matching group "$1" meaning the first group, "$2" the second, and so on.' -
Look i work on windows servers so i dont use htaccess, so i am no expert with htaccess, but I do know regex pretty well,
I think you have it wrong in 2 ways.
- you are directed from non www. to www the wrong way.
Also the rewrite condition is a RegEx, it needs to be escaped
so i think you should have
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
This is all so much easier on windows servers
-
Is this correct
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
What does $1 mean ?
-
You've just missed the "on" at the end of the first line and have it at the start of the 2nd I think (not sure if that matters). So,
RewriteEngine On
And then take the www off the second line. So,
RewriteCond %{HTTP_HOST} ^example.com
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Best Practice for www and non www
How is the best way to handle all the different variations of a website in terms of www | non www | http | https? In Google Search Console, I have all 4 versions and I have selected a preference. In Open Site Explorer I can see that the www and non www versions are treated differently with one group of links pointing to each version of the same page. This gives a different PA score. eg. http://mydomain.com DA 25 PA 35 http://www.mydomain.com DA 19 PA 21 Each version of the home page having it's only set of links and scores. Should I try and "consolidate" all the scores into one page? Should I set up redirects to my preferred version of the website? Thanks in advance
Technical SEO | | I.AM.Strategist0 -
Hi! I'm wondering whether for keyword SEO - a url should be www.salshoes.com/shoes/mens/day-wear (so with a few parent categories) or www.salshoes.com/shoes-mens-day-wear is ok for on page optimization?
Hi! I'm wondering whether for keyword SEO - a url should be www.salshoes.com/shoes/mens/day-wear (so with a few parent categories) or www.salshoes.com/shoes-mens-day-wear is ok for on page optimization? Hi! I'm wondering whether for keyword SEO - a url should be www.salshoes.com/shoes/mens/day-wear (so with a few parent categories) or www.salshoes.com/shoes-mens-day-wear is ok for on page optimization?
Technical SEO | | SalSantaCruz0 -
Do I need to verify my site on webmaster both with and without the "www." at the start?
As per title, is it necessary to verify a site on webmaster twice, with and without the "www"? I only ask as I'm about to submit a disavow request, and have just read this: NB: Make sure you verify both the http:website.com and http://www.website.com versions of your site and submit the links disavow file for each. Google has said that they view these as completely different sites so it’s important not to forget this step. (here) Is there anything in this? It strikes me as more than a bit odd that you need to submit a site twice.
Technical SEO | | mgane0 -
Non WWW. versus WWW. versions, current best practice ?
Hi Im increasingly seeing sites not using the www., but understand from various sources including seomoz that best practice is to be on the www. with the non www version 301'd to the www version. Since alot of sites are clearly doing this the other way round now is that better practice or the former still best ? I appreciate that non www version gives you 3 more characters for url's but apart from that is there any benefit over the www. version ? Cheers Dan
Technical SEO | | Dan-Lawrence0 -
Wordpress & use of 'www' vs not for webmaster tools - explanation needed
I am having a hard time understanding the issue of canonization of site pages, specifically in regards to the 'www' or 'non-www' versions of a site. And specifically in regards to wordpress. I can see that it doesn't matter whether you type in 'www' or not in the url for a wordpress site, what is going on in the back end that allows this? When I link up to google webmaster tools, should i use www or not? thanks for any help d
Technical SEO | | dnaynay0 -
Non-www to www code not working in htaccess
I use the same rewrite code on every site to consolidate the non-www and www versions. All sites in Joomla, linux hosting. Code is as follows: RewriteEngine On rewritecond %{http_host} ^site.com/ rewriteRule ^(.*) http://www.site.com/$1 [R=301,L] Immediately following this code, I also rewrite /index.php to /. Thing is, I can get index.php to rewrite correctly but the non-www won't rewrite to www. I use the same code on every site but for some reason it's not working here. Are there common issues that interfere with rewriting a non-www to www in htaccess that could be interfering with the code I'm using above?
Technical SEO | | Caleone0 -
For SERPS which is better www or non www
Does it matter if i use to www or no www for SERPS on google? Obviously the redirection would be set up for either in the htaccess file.
Technical SEO | | apwade0 -
Www/nonwww .co.uk/.com
When I started SEO - I didn't really know what I was doing (still don't!) Just wondering if anyone can help me with this small problem. I now understand that I basically have 4 URLs www.ablemagazine.com (Page Authority: 38/100) www.ablemagazine.co.uk (Page Authority: 47/100) ablemagazine.com (Page Authority: 3/100) ablemagazine.co.uk (Page Authority: 51/100) What should be configuration be to ensure I'm not loosing masses amounts of linkjuice? At the moment I have ablemagazine.co.uk set as my default domain in webmaster tools. www.ablemagazine.com www.ablemagazine.co.uk and ablemagazine.com all 301 redirect here (I think)
Technical SEO | | craven220