Redirecting non-www to www with .htaccess
080227
If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This will redirect any requests to http://my-domain.com to http://www.my-domain.com. There are several benefits from doing that:
- It will avoid duplicate content in Google
- It will avoid the possibility of split page rank and/or split link popularity (inbound links).
- It's nicer, and more consistent.
Note that if your site has already been indexed by Google without the www, this might cause unwanted side effects, like lost of PR. I don't think this would happen, or in any case it would be a temporary issue (we are doing a permanent redirect, 301, so Google should transfer all rankings to the www version). But anyway, use at your own risk!
Something nice about the code above is that you can use it for any website, since it doesn't include the actual domain name.
Redirecting www to non-www
If you want to do the opposite, the code is very similar:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]
In this case we are explicitly typing the domain name. I'm sure it's possible to do it in a generic way, but I haven't had the time to work one out and test it. So remember to change 'my-domain' with your domain name!
Posted in: English, Web, .htaccess
Tags: mod_rewrite, seo
Would you mind explaining exactly what each part of those two lines does? :)
Always been curious.
Sure! The first line sets a condition: only if the condition is true, the second line will be processed. The condition could be 'translated' to: "if the host name doesn't start with www.". The regular expression !^www\. means this:
! = not
^ = start
\. = . (the backslash is the escape character, because dots have a special meaning in regular expressions, and therefore must be escaped)
So !^www\. means "doesn't start with www.".
The second line is the actual rewrite rule: again it uses regular expressions to match certain urls, and then rewrites them to something else. The first part is the regular expression:
^(.*)$
This means: anything! You already know the ^ sign. The (.*) bit means zero or more characters (the dot means any character, the asterisk means zero or more). The final $ means 'end'.
Then comes the bit that says how to rewrite the url:
http://www.%{HTTP_HOST}/$1 [R=301,L]
%{HTTP_HOST} will be replaced by the host name (i.e. dense13.com).
$1 references whatever was matched in the regular expression between the brackets, which in this case is everything.
The [R=301,L] means "inform the user agent that this is a permanent redirect (HTTP 301 code), and don't process any more rewrite rules (if there were any after this one).
If you're not familiar with regular expressions, this might still look a bit abstract, feel free to ask for more details. :)
Mmmh, I need to do some more work on the blog, I don't like how it automatically adds fancy quotes and converts text that looks like a url into a link.
Y si quisieras hacerlo al reves? Que todo lo que sea dense13.com vaya a www.dense13.com ?
Hi Natalia,
I assume you mean how to redirect from the www version to the non-www version. I've updated the post to include that option as well.
If you're curious, the only thing that's new in that code is the [NC], which stands for no-case (I think). That means that the pattern before will be case-insensitive, and thus will match my-domain.com, My-Domain.com, mY-dOMaiN.CoM, etc.
So I got confused because I types www.dense13.com and it sent me to dense13.com so I assumed you were explaining how to do that www to non-www even though the title says the opposite ... yep, I know how that sounds, don't judge :P
That is also why the actual explanation didn't seem to make a lot of sense :lol:
I actually had to change a .htaccess file without having a clue about the whole thing and somehow made it work, I seem to have some inner intuition that lets me do things my brain doesn't quite grasp.
Minor thing...Comments in reverse order are kind confusing, with posts it works cos one doesn't follow from the previous one, or if it does they are normally linked, comments on the other hand...especially long ones...one has to keep scrolling up and down to follow the conversation if you know what I mean.
You're right, dense13.com uses the www to non-www method, which is confusing, good point. I might reverse the order of the two methods in the post, to avoid confusion.
I kind of like reversed order also in comments, mmmh, I'll have to think about that one.
Wo-hoo! I managed to disable curly quotes and auto-links in comments! :)
Hi, Great tutorial, thanks. Also, thanks for taking time to explain
what exactly each of those symbols meant :) One question. After
using the non-www to www redirect, it redirects my site to the www
version just fine. But the thing is, if I enter in
site.com/somepage.html , it redirects me to www.site.com and not
www.site.com/somepage.html. Is there any way to make it do the
latter? Thanks in advance :) Nithanth
Hi Nithanth. That's strange, it should be redirecting you to the right page. First thing would be to make sure you typed the right code in your .htaccess, which you've probably done already. Feel free to paste your htaccess code here or mail it to me at gperezdelaossa_AT_dense13_DOT_com . I don't know if I'll be able to help, but I can try. :)
Hi, This is how my htaccess currently looks: [code] # -FrontPage-
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
order deny,allow deny from all allow from all order deny,allow deny
from all AuthName nithanth.net AuthUserFile
/home/nithanth/public_html/_vti_pvt/service.pwd RewriteEngine On
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] RewriteEngine
On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$
http://www.%{HTTP_HOST}/$1 [R=301,L] [/code] Site is nithanth.net.
Try http://nithanth.net/general/hello-world/ - it doesn't take me
to http://www.nithanth.net/general/hello-world/ , instead it takes
me to http://www.nithanth.net/index.php :(
Mmh, I'll have to offer a way for people to post code, it's all messed up, sorry! I'll try to re-format it, and have a look. I might not be able to do it straight away though, I'm going through a bit of a busy period right now, but will let you know when I do.
The code works perfectly, thank you.
thanks for the code, it's all that i need
Unfortunately this method messes up SSL URLs. https://my-domain.com
gets redirected to: http://www.my-domain.com Not good. :(
You could create a second .htaccess file in the folder(s) where your SSL pages are, using replacing http for https. I haven't actually tried this, but I'd say it should work.
Actually just add this line to the conditions: RewriteCond
%{SERVER_PORT} ^80$ Assuming all your http traffic is on port 80
this will do the trick.
Oh and do a separate condition/rule for port 443 (SSL)
That's neater, good thinking.
Great tip, thanks! No more putting full domain name in the
.htaccess file :)
Thanks very much for this post. I created a .htaccess file with
your code copy-pasted for website www.limlum.com. When I type
limlum.com in the web browser address, it redirects to
http://www.limlum.com/public_html/ which does not exist and I get a
404 error. Am I missing another file on the webste?
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Sorry I posted before testing properly - I put the .htaccess file
in a directory one level above public_html which is the wrong
location. I put it in public_html and it now works. Thanks.
Bro, what about having subdomains? I used your code but now this
code is forwarding my subdomain to a www site as well. So how do
you add an exception to the code for a subdomain?
Nice code. Works fine but what about if someone is running
subdomains? The code forwards my forum sub domain to www.forum. ...
etc. So how can I add an exception to the code not to touch the sub
domains?
@Asak Kay: just change the Rewrite condition to include any other subdomains besides www:
RewriteCond %{HTTP_HOST} !^(www|sub1|sub2)\.
That should do it.
I's supposed you will need to write RewriteRule ^(.*)$
http://www\.%{HTTP_HOST}/$1 [R=301,L,NC] instead of RewriteRule
^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] I'm add /. and NC, nc
here for no case option, so you can write your domain in any case
Am I correct?
@nyha: I dont think you need to escape the period in the second part of the RewriteRule, that's not a regexp. And not sure about the NC, I think that's to use in the condition (as in 'Redirecting www to non-www' in the post). But might be worth testing, I'll see if I can do it some time soon.
Really nice explanation for redirect condition.
Thanks for this post. Having just changed webhosts and the new one
not offering a GUI for updating the www to non-www redirect and
thanks to GOOG I've landed here and am now learning the basics of
.htaccess editing (secretly, it tickles the geek in me :) Thanks,
also, for explaining the entry. As easy as it is to just cut and
paste something I, personally, prefer to understand how to do it.
Although I think I'll have to re-read it a few more times first.
Once again, cheers. :)
Thank you so much for this post !! I had the same problem as Asad
Kay (in comment nr.24) with the sub domains and your solution (in
comment nr.25) was a life saver for me !! It's very simple when you
think about it, but I didn't and was struggling with it for half a
day !! Thanks again !!
Great post, thanks a lot!! I had an error in my code and could not
figure out why I got an interal server error until I found this
post. Just copied the code and pasted into my htaccess and it works
perfectly. Again, thanks for the post :)
Thank you for this great post.Finally found what i need to redirect
all of my pages non www to www.Really helpful!
Thanks for help!!!!!!!!!!!!!!
[...] For non-www to www [...]
Hi, What is better for SEO www to non-www or non-www to www? I have
read that one way if good for SEO but the other not. What is the
best way? Thanks
@Laurent: I don't know, you'll have to look for some SEO specific resource. I suspect it won't make any difference, but that's just my intuitive opinion.
Hi, I would like to use mod rewrite to redirect domain-N.tld to
www.domain-N.tld (1.) sub-M.domain-N.tld to sub-M.domain-N.tld
(keep the original, don't add www). (~ if the request is domain.tld
-> rewrite, else keep) I have over 100 domains, so N,M>100,
and I need regexp. (1.) works fine, but I can't remove the www from
the subdomains. If you can, please help me. Thx.
Thanks so much for the right code for non-www to www. I have been
trying for a week to get my site to redirect. I have searched many
sites for advice, but unfortunately nothing worked until I put your
code in. I added [NC] after "RewriteCond %{HTTP_HOST} !^www\.
[NC]". Works great! Thanks again.
Hi, I would like to use mod rewrite to redirect
mysite.com/index.php to www.mysite.com ,but I can't ! If you can,
please help me. Thank's...
Hello, thanks for your excellent site. Perhaps you can help with my
problem. Any help is appreciated! I want to redirect my /index.html
to my "/" (www.sample.com). There is only one domain on the server.
I am using the following script: RewriteEngine On RewriteCond
%{THE_REQUEST} /index\.html\ HTTP/ RewriteRule ^index\.html$ /
[R=301,L] It works, but if you type other things in your browser,
it brings up a webpage but without all the shapes/pics/etc. For
example: www.sample.com/paper/index.html would bring up the webpage
www.sample.com/paper with the text, but without the shapes, pics,
etc. I want the .htaccess to redirect those types of requests to
the www.sample.com/ page (and of course, without the .index.) I
tried: RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\
/(([^/]+/)*)index\.html\ HTTP/ RewriteRule index\.html$
http://www.example.com/%1 [R=301,L] It works about the same as the
first example.
very nice solution through .htacess. I was looking for some plugin
for to do the job of shifting www to non www
Very helpful... I was able to redirect my main blog but unable to
redirect the addon domain to www version.
[...] pour n’importe quel site, puisque le nom de domaine n’est pas
dedans.Via : Redirecting non-www to www with .htaccess Image via :
DatacenterArticles RelatifsFaire détecter son Eee par sa Freebox3
commandes à ne pas [...]
Thank you! Someone gave me the opposite code! >:(
Hi there, What code do i need to program to redirect ONLY
http://mydomain.com to http://www.mydomain.com i tested the first
one in this topic and it went good but it went wrong when i
navigate to my ssl url https://secure.mydomain.com can anyone help
me with this?
@MiC: check out comments 15 to 17. Try what John M suggests in 17, I haven't tested it myself but it should work.
Hi, I got a queriy and wanted your help in this regard. When people
access my domain it is redirected to
http://www.mydomain.com/en/index.php file using php code.I used
RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$
http://www.%{HTTP_HOST}/$1 [R=301,L] to redirect people from non
www to www but its not working. Still user can access
http://mydomain.com/en/index.php
@Anand: that code should work, there might be other parts of your .htaccess file influencing it (assuming you are using an .htaccess file). I suggest commenting things out and make sure the redirection is working correctly, and then bring things back step by step, to locate the problem. You might also want to post to a forum, including your .htaccess code (I'm keen to help when I can, but have limited time).
Hi My rule for redirecting making flat links works, but how do i
combine the two rules please?? RewriteEngine on RewriteRule
^page/([a-z]+) home.php?page=$1 [NC] RewriteCond %{HTTP_HOST}
^domain\.co\.uk [nc] RewriteRule (.*) http://www.domain.co.uk/$1
[R=301,L]
@MartynG: first do the www redirection, then send everything to home.php.
hi dense13, can you tell me if your code to redirect(non-www to
www) will work on an iis6 server without using any kind of plugins
or other installations (ie. heliotech - isapi rewrite)? our pages
are coming from an apache server to the iis6 server. thank you
@cyndi: I don't think so, but I don't know much about IIS.
can you help me to resolve the error at
http://www.kerjatop.com/sitemap.xml otherwise but there is no error
with http://kerjatop.com/sitemap.xml
@heru: what redirection are you using? What are you trying to do exactly?
Excelent it works to me, i was lookin' all over internet, and i
finally get! It's a got idea to redirect to only one page!
Thanks so much! What a life-saver.
Thanks so much, looking for this for ages :D
Question concerning non-www to www. I have a site in which a
discussion forum resides in one of the sub-directories. The
standard rewrite plays havoc with the Forum but I still want to use
the redirect on the root directory only. Is it possible rewrite
non-www to www for the root directory only? Exclude rewrites for
sub-directories?
How to add the code in the wordpress installation? my site got
error when adding the code. # BEGIN WordPress RewriteEngine On
RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond
%{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L] # END WordPress
Perfect formula, since it's generic!
Working for me! Thank you very much!
Thanks for nice post. I've implemented this on my web. It redirect
from mydomain.com to www.mydomain.com. but when I try to open
directly from URL: mydomain.com/somedirectory/somepage.htm it still
not redirected to www.mydomain.com/somedirectory/somepage.htm Can
you help me how it could redirected as well?
I've tried this method but i was still confused use this in another
directory..
Hi this is my .htaccess file in joomla and sends people to my new
site and it also redirects www to non www it works fine but how do
I make a permanent (301) Redirect RewriteEngine on RewriteCond
%{REQUEST_FILENAME} !-f RewriteRule ^(.*)index.shtml$
http://www.mynewsite.com/index.php RewriteRule ^/(.*)$
http://www.mynewsite.com/index.php
Hey, just wanted to say thanks! After reading over many tutorials
on how to go about this I wasn't fully understanding the method
(many kept trying to input URL's as well and I needed a generic
method). This worked flawlessly, :).
Hello, Thanks for your tutorial. It's working fine. But I need my
site to be redirected to ROOT for all version of index.php. For
example if I type http://www.grantmarketinggroup.com/index.php it
will be redirected to http://www.grantmarketinggroup.com/ . I have
tried lots of rewrite rules but no results. Hope you will find time
to respond me. Thanks again. Gowranga
thanks brooo
I'm having issue while trying to redirect non www to www in
wordpress. it gives redirect loop errror :(
It would be great if you can also create an IIS version for non-www
to www redirection. Not everyone uses Apache as a web server.
when I do this, all I ever get is an endless browser redirect
warning (i'm using WordPress also): I have this: #non-www to www
start RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule
^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] #non-www to www end #
BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule
^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END
WordPress any ideas??
after creating .htaccess with above information to convert www to
non www my site traffic decreased to 50% in 1 day and in webmaster
page index its showing pending. i have undo the changes still i
cannot see any change in traffic its going on decrease..any idea
again when my results will be normal in google ?
Sorry renuka, I can't help with that. I'd go to a SEO-specific forum and ask there. I hope you can sort that out quickly.
Thank you so very much for this information and thorough
explanation on how to redirect www to non-www and vice-versa with
htacess. I don't have subdomains so far, but I thought I'd
implement the line on Comment #25 just in case. But when I changed
RewriteCond %{HTTP_HOST} !^www\.
into RewriteCond%{HTTP_HOST} !^(www|sub1|sub2)\., the redirecting stopped.
Redirecting resumed as soon as I switched back to the original
line. Any ideas why? Should I have added the line in Comment #25
instead of substituting? Thanks.
very nice solution through .htacess.
Wondering if it's possible to redirect a website folder link that
comes before another one - so, redirecting without effecting any
further sub folders links? For instance, I have a page called
"www.xxx.com/abc/1" But I don't use /abc/ folder anymore and want
to redirect it to /def/ folder instead. So, www.xxx.com/abc/
redirects to www.to xxx.com/def/ But when I create a .htaccess with
the codes below; URL PERMANENT REDIRECT Redirect permanent /abc
http://www.xxx.com/def Then, abc/1/ folder also wants to redirect
to /def/1/ Since there is no page called def/1/ on my server and no
chance to create/move the page due to some complexities, how do you
think I can resolve this issue with .htaccess? Thanks.
Sorry about all the people asking for help, I'd like to help out but I don't have much time available now. I will try to find some, but can't guarantee...
Great post... thanks for sharing! I am having a problem with this
and was hoping you could give me a steer in the right direction.
When I upload the .htaccess file and then enter in the url without
the URL e.g website.co.nz the browser is redirecting to
www.website.co.nz/www/htdocs/ I cannot work out where it is picking
up the /www/htdocs element from. Can you shed any light?
Does it matter what kind of software you are running in order to
this. If it is a wordpress site, don't they have a plugin for
something like this?
Thank you, great code! But unfortunately, the code doesn't work for
me. I'm having a multilingual blog with different domains for each
language. The settings are that the second language is a server
alias (mirror of the main domain). The redirecting works "kind of"
but brings the content in one language and sidebar/header in the
other language.
Thank you! This just helped my website to avoid duplicate and
splitting the rank.
Nice post... I really need for my web... Thanks for your share.
i added this code: RewriteEngine On RewriteCond %{HTTP_HOST}
!^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] on
file .htacess. but it's not work.
@Ronan it seems you put your .htaccess in wrong location read
comment no 21 and 22 put your .htaccess file in correct directory
Hello webmaster i need some help regarding the non www to www
redirection. I placed the below code in the domain.com but its not
redirecting RewriteEngine on rewritecond %{http_host} ^domain.com
[nc] rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] Please
suggest me regarding this
Hi admin How to redirect entire website to home page i tried many
ways but i didn't succeed can you please help on this
Thanks for htaccess :) It saved me 5 minutes (:
Ha, thank you very much for the tutorial. I've used this similar
htaccess redirection technique several times, it's so useful :)
how can we redirect domain1.com to domain2.com ? i have own both
domain but don't know how to redirect.
Hi Rahu, Use the following code to redirect from one domain to
another : RewriteEngine on RewriteCond %{HTTP_HOST}
^www.old-domain.com$ [NC] RewriteRule ^(.*)$
http://www.new-domain.com/$1 [R=301,L]
One thing I don't get is the backreference. If (.*) matches the
entire URL, then shouldn't the resulting URL after the rewrite look
like: http://www.somesite.net/http://somesite.net?? According to
official Appache documentation the pattern is tested against the
whole URL, but it behaves as if it is only testing the URI.
@Daniel, from the mod_rewrite docs:
"In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html")."
Hi' I hv problem with www and non www. my htaccess is below:
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule
without www is ok. but withh www.domain name.com^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END
WordPress
not working... what sould i do for both working (www and non-www) I
read your article but I dont understand .... please help me
I'm sorry about the poor formatting of code in the comments, I really should fix that.
Hi, Great!!! It works... It is redirecting as I want. But I
couldn't login to my admin and members also can't login with their
old login details. How to fix it? Thanks, Jasin A.
Thank you.
The code perfectly worked on my website. I believe redirecting
non-www to www is a great way to not loosing pagerank
Thanks lot i am thankful to you. you helped me lot. your
information is worth to know. i am able to redirect the url after 3
day.
Nice post - very clearly explained. I want to rewrite
http://thecoffeelocator.com to http://www.thecoffeelocator.com and
have used the following: RewriteEngine On RewriteCond %{HTTP_HOST}
^http://thecoffeelocator\.com [NC] RewriteRule ^(.*)$
http://www\.thecoffeelocator.com/$1 [L,R=301] But this is not
working. Do you have any ideas? Thanks
Apologies. That has just started working. There must be a time
delay between loading the file and server recognising the htaccess!
Thanks for your post anyway
Excellent. I have been trying for about an hour to find the correct
code to do this and yours is the first successful method with by
far the neatest solution too. Thank you
Thanks for the Clear info. For those who changing .htaccess in
Opencart application : Dont forget to change your site url to www.
in config.php in root folder and config.php /admin/ folder.
could you look into this and tell me my mistake?
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [R,NE]
RewriteCond %{HTTP_HOST} ^(www.)?mywebsite.(com|co\.uk)$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?mywebsite.(com|co\.uk)$
RewriteRule ^(/)?$ www/index.html [L]
thank you!
good working....thank you
thanks alot brother,,,,
It works, great, thanks! Now I am struggling with a file extension
modifier code... hmm won't work!
I have used your code above to apply redirections from non-www to www. However, how to redirect http://www.example.com/index.html to http://www.example.com. This question was also asked by Gowranga Chintapatra in comment 66.
@Naveen: since that's a different issue, I wrote a whole post about it. :)
Removing index.html with mod_rewrite in .htaccess
Thanks,
This helped a lot!
One problem with our caching in Joomla is that it's caching pages as anything anyone inputs to get to the site, so that if some intputs blahblah.domain.com/whatever people get served that URL next time they visit.
Your fix for non www to www works to an extent, but in this instance what we get is www.blahblah.domain.com.
Plus if people use a re-direct, let's say domain.es, this gets served too.
I've tried searching everywhere and can't come up with a fix - so is there something that can re-direct and blahblah or TLD to good old domain.com/whatever.
Thanks for your time and an extremely useful post,
Dominic
The www to non www htacess redirect is not working for me. I don't know that reason for that :(
Thanks for the post. I am looking to redirect ip address to domain. How can we do that?
this is very good. even now a lot of admins dont know the the htaccess redirection. htaccess is very helpful in seo aspect, which removes the site and content duplication by redirecting the non www to www versions and index to root , custom error page and many more useful redirections.. Thanks for giving the htaccess code
How would you recommend removing the /index.html part from the http://www.host.com/index.html url.
Thank you very much.
Just wanted to add that I modified the rule so that it isn't specific to protocol for the redirect:
RewriteRule ^(.*)$ %{SERVER_PROTOCOL}://www.%{HTTP_HOST}/$1 [R=301,L]
The code works perfectly, thank you.
Hi, I'm not able to redirect the whole site to www version. The main page redirects perfectly ( example.com to www.example.com)
But if I try another page or directory, it does not redirect, and displays the same non-www url in the address bar(example.com/services DOESN'T redirect to www.example.com/services, which I need) . Please help me I have tried everycombination but it doesnt seem to work :(
Ok I played with htaccess a little and now i am able to redirect the subpages without www to the homepage with www, but now stuck here, tried everything..
for exampl, example.com/services redirects to www.example.com/index.php :S Please help
i am not able to get the non-www to redirect to www.
i aplied the changes above to do so and got a server error taking my www.mediasolvcorp.com site down.
not sure what i did wrong, can u check my code changes and see if you can tell where i may have screwed up?
here is original htaccess working code:(just from the tag)
RewriteEngine On
RewriteBase /mediasolv/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mediasolv/index.php [L]
In order to get non www to direct to www using your fix I changed to:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Brought down the site.
what haven if i do not redirect non www to www? flagged as duplicate content by Google?
I am a newbie on wordpress
Where i put the above code on htaccess( which line)???
Works!!!! Thanks!
Great thanks for the info. Great that both redirects are listed.
Also good to add if people are getting crawl errors for old pages that they have renamed and have backlinks to:
RewriteRule ^about_us.php$ "http://www.yourdomain/about_us.htm" [R=301,L]
Which will redirect the old "about us" page to the new page.
When I use this code on a WordPress site, I put it directly above the wordpress info in htacces and it works perfectly
Work's just as advertised, and was very well explained in the comments very early on. Thanks for this post, it's still very helpful!
Is there also another way?
Dense. I need www to non www version of all urls. Please provide to us a version for this too.. I know that it must be easy.. but i dont want to broke anything.. i'm new to this.
This should be ok ?
RewriteEngine On
RewriteCond %{HTTP_HOST} http://www.%{HTTP_HOST}/
RewriteRule ^(.*)$ !^www\.$1 [R=301,L]
I have a .htaccess redirect file to convert all variations of my url to be just the one, eg, to change eg, url/index.html, www.url.com.au, etc.
However it seems to change them all to no http or www. Now I want to install wordpress on a subdomain blog, it wont work because of the redirects. WordPress requires a http://www.url.com.au to work. Any idea if I can adjust the.htaccess code a little to redirect all variations to have a http://www.url.com.au
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.url.com.au/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^url.com.au$
RewriteRule (.*) http://www.url.com.au/$1 [R=301,L]
can we use php redirect instead of htaccess way?
Urgent:
why my home is redirecting to my-domain.com even after i did correction
replace this code
----------
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]
--------------
with
RewriteEngine On
RewriteCond %{HTTP_HOST} !^shoppingmart.comyr.com$ [NC]
RewriteRule ^(.*)$ http://shoppingmart.comyr.com/$1 [R=301,L]
after that, default, my-domain.com is opening....why, tell me urgently
So I did this and it did change it to a WWW host but now it does
this: http://www.www.mysticania.x10.mx/
Thanks, After setting non-www to www, i don't know why but site
loading more faster. I like first code because it has no site
related issue i mean site url. Tanks again..
Thanks for the article, much appreciated. Definitely don't want
duplicate content on Google.
Thank you. Have just set up a htaccess and followed all to the
www.site...it workes great thank you!!
Thank you! It works like a charm!
1 down vote I ran this... RewriteEngine on RewriteCond %{HTTP_HOST}
!^www.*$ [NC] RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/$1
[R=301,L] I need this to be universal for 25+ domains on our new
server, so this directive is in my virtual.conf file in a tag. (dir
is parent to all docroots) I had to do a bit of a hack on the
rewrite rule though, as the full docroot was being carried through
on the pattern match, despite what
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html says about it
only being stuff after the host and port.
great info, i was stuck on this for ages, many thanks, regards
chris
thank you very much for your article, plz i need how to redirect
all site to https? i wait your repley Regards
Hi, My wordpress website was hacked, and when I access it without
www, it brings to a hacked page. So I've thought adding this line
of code to .htaccess but there are some lines of code put by
wordpress. Should I delete them and use your code? WORDPRESS CODE:
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule
^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END
WordPress --------------------------------------------------- YOUR
CODE: RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule
^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Thank you a lot for this! It really should be the first thing one
finds if one Googles for redirecting a naked domain to www. I had
some odd issues with cookie management when users came from the
naked domain that I really didn't want to debug. So now I don't
have to. And that is in addition to the perks you already mentioned
in your article. AND I don't have to pay for domain forwarding. So
great! :)
Great post (and comments, especially the one explaining the regex).
I was looking for a way to redirect visitors who typed in the ip of
the server instead of my domain name. Example:
http://123.123.123.123 would also display my site and I wanted to
make sure those visitors were redirected to
http://www.mydomain.info instead. Thanks to you I managed to figure
it out. Thank you.
Nice, thanks for this htacess tricks, usefull. Prevent from
duplicate content.
hy everyone! i use this to redirect all www pages to non-www
RewriteEngine On RewriteCond %{HTTP_HOST} !^my-domain.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L] but...i have
subdomains activated on my server if someone type
www.subdomain.domain.com redirect to domain.com i want to redirect
to subdomain.domain.com have a solution for me? thank you!
I buid phanhuucovisinh.vn, it default non-www, i want www, but i
don't see .htacess
Hi there! I have my site in blogger. If I redirect, all the links
to files in my server break... Is there a way to include an
exception for specific folders like domain.com/images/picture.jpg
so they are not redirected? Thank you!
Excellent, in just 15mins I am able to understand & integrate
this. THank you for the quick & simplest solution you are
giving.
Thank you for the article and explanation. It helps to understand
rewrite rule in detail.
Now I'm getting this error ERR_TOO_MANY_REDIRECTS
If your site is set to force https RewriteRule ^(.*)$
https://www.%{HTTP_HOST}/$1 [R=301,L] before the http to https
rules
Hello i am using Amazon EC2 as my server and PHP Dolphin as a
social networking script and i have used this code this code
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule
^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] but its not working on
this so kindly suggest me how to solve this problem
FYI: You can also use http://redirect-www.org to handle the
redirect for you if you have access to DNS but not .htaccess (or
are just having trouble with it).
Hi , I have this rewrite engine for converting http requests to
https.When i type it converts to https://www.domain.in but when i
type https:domain.in it is not rediected to https://www.domain.in
How i need to get this worked RewriteEngine On RewriteCond %{HTTPS}
off RewriteRule ^(.*)$ https://domain$1 RewriteRule (.*)
https://%{HTTP_HOST}%{REQUEST_URI}
Becareful not to redirect https to http!!
RewriteCond
%{HTTPS} !=on RewriteCond %{HTTP_HOST} !^www..+$ [NC] RewriteRule
^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I am Using Blogger . How can Use this Code .
I want to redirect to https://www.website.com/ I did the following
combo. RewriteCond %{HTTP_HOST} !^www. RewriteRule ^(.*)$
https://www.%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{SERVER_PORT}
!^443$ RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
[R=301,L] It works, but is it correct?
I wanted to thank you for your explanation of this .htaccess 301
redirect as although my wordpress setup was using the www directive
in the site url the domain level incoming links were still being
attributed to the non www version. All wordpress users need to be
aware that prior domain owners may have sent links to the non www
domain and you should use the information in this article to make
sure you can credit that old link juice back to your www domain.
Hello Thanks for the guide! how I can move multiple url, s from one
domain to another? example: www.example.com to www.example.es -
www.example.com/contact.php to www.example.es/contect.php Thanks!!
One or two people might be interested in this: Remove "www" from
any URL without affecting it's HTTP or HTTPS from the org. request.
(I needed to remove www while leaving intact that some pages on the
site should open in http, others in httpS. RewriteCond %{HTTP_HOST}
^www\. RewriteCond %{HTTPS}s ^on(s)|off RewriteCond
http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$ RewriteRule ^
%1%3%{REQUEST_URI} [R=301,L] Tested and works wonderful. - Blauwe
Hi, I am trying to redirect an xml sitemap from www to non-www.
from http://www.dentistinflushingny.com/sitemap_index.xml to
http://dentistinflushingny.com/sitemap_index.xml. I've tried the
example above but it doesn't seem to work. Here is my code below:
RewriteEngine On RewriteCond %{HTTP_HOST}
^www\.dentistinflushingny\.com/sitemap_index\.xml$ [NC] RewriteRule
^(.*)$ http://dentistinflushingny.com/sitemap_index.xml$1
[L,R=301]
@dense13 Just thought you should know that I'm still coming back
here, 5 years after leaving my first comment (#29), saying thanks.
Manual rewrites is something I do every once in a while, and I
forget, and this article's always first in the SERPS. it's the gift
that keeps on giving!!! Kudos to you my friend.
Thanks @phil.t!
There were a few people above who were having issues with redirect
loops. I had this issue as well. The solution for me was in my
wordpress settings. On the General Settings page of the wordpress
config, set the site url and wordpress url to the www version. I'm
guessing that this was redirecting back to the non-www version,
hence the loop. Hope that helps someone Dave
I've got a question here regarding website redirection from non www
to www. Currently, I have this on my htaccess file: Options
-Indexes RewriteEngine on RewriteBase / RewriteRule ^index\.php$ -
[L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] ErrorDocument
404 http://our-website.com RewriteCond %{HTTP_HOST}
!^www.our-website.com$ RewriteRule ^(.*)$
http://www.our-website.com/$1 [R=301] our-website.com is currently
stored in public_html folder and we have a sub-domain named as
events.our-website.com and it's folder stored in
public_html\ourevents subfolder. I had it's domain correctly
configured with godaddy as event.our-website.com. Now, why is it
that everytime I try to browse event.ourwebsite.com, it redirects
automatically to our-website.com/ourevents shich shouldn't be the
case. I commented out: RewriteCond %{HTTP_HOST}
!^www.our-website.com$ RewriteRule ^(.*)$
http://www.our-website.com/$1 [R=301] on .htaccess file and it's
working fine. I don't want those 2 lines of codes removed for our
Google analytic functionality to work so is there are way to get
around with this? Thank you.
@dense13 First I would like to chime in my thanks as well - I often
have to monkey around with .htaccess files, and I'm grateful your
post came up so high in the SERPs for my search (about redirecting
non-www to www) - it's the best explanation I've found....your
comment that breaks down those few lines into what each does was
very educational....I now understand what before I just copied and
pasted and crossed my fingers in hope that it would work. I do have
a question though.....about this bit where you explain the [301,L]
I do understand 301 redirects (vs. 302, etc.) but where you say
"[...]and don't process any more rewrite rules (if there were any
after this one)." I have these lines at the very top of my
.htaccess file, but following them are some stuff that WordPress
adds, including a couple of other 'rewrite' rules.....so does the
"L" mean that those additional rewrite rules will be ignored? If
so, how to I change this, maybe just leave off the "L"? Your
thoughts would most appreciated!
@Trisha: the rewrite rules only take effect when the previous condition is met, otherwise no rewrite takes place and the following WP code gets evaluated. I can't imagine why you'd want to leave off the "L": if a 301 redirect is taking place (because the condition is met), there's no need to continue processing the file. A new request will be made and that's the one that your WP code will use.
Btw, a general message to future readers: I ge a lot of spam on this post, and mixed with it quite a few questions about specific scenarios. Sometimes I might be able to answer small things like Trisha's here, but generally I can't provide support for every custom situation. If you have trouble with .htaccess redirection you'll probably be better off going to stackoverflow or some other forum. Not trying to be rude, just so that you don't get frustrated if there's no answer here. :)
@dense13, once again thank you for a helpful clarification......I
will leave the L in as you have it. :-)
Fantastic post thank you - cleared up a real headache i've been working through on the .htaccess
Appreciate you are busy just wondered if now that google penalises non ssl sites if there was a clean way to redirect all non www traffic to https:// as well as redirect http to https without creating a loop
thanks again - i'm playing with the port conditions but would assume as this is the cleanest / best answer and works great there are going to be a lot of people who'd love that update - thanks again