You should really be doing one or the other. For consistency, as well as SEO’s, sake.
Force the www.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-site.com [NC]
RewriteRule ^(.*)$ http://www.your-site.com/$1 [L,R=301]
Remove the www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]
Funnily enough, I was just looking for this… Thanks!
And why this site is not working without a “www”.
http://znanje.org/
you have to put www to load the page
http://www.znanje.org/
A and Aname?
It depends on the server configuration. Most server will work without www, but I come across others once in a while.
It is working on my website. Check for the configuration above once again.
Well you seem to have some issues with your DNS records. Run a check and it should work!
You should add a ServerAlias to the apache file (assuming the site is server by apache) like to:
DocumentRoot /path/to/website/files
ServerAlias www.
ServerName zanje.org
Will this also redirect https://yourdomain.com to https://www.yourdomain.com ?
Won’t this remove any subdomain? Not just the www. Just wondering.
Nope, subdomains should be fine.
I just put this on my clients site and now its not working. They are a major manufacturer and this is freaking me out!!
It is working on my website, getback with your file. Write here and would check it.
Was not a smart move to deploy something before testing it first.
Simply a thing: it requires that Apache has enabled the rewrite module. You can ask your host for some information.
#nearly universal www – Resolve (takes away WWW from the URL on both HTTP and HTTPS)
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
When I try this in my .htaccess, the path is being removed:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
This is what I get: http://www.site.com/rulezz -> site.com/
instead of what I’d like: site.com/rulezz
Any ideas?
Hi,
Removing www from the url of the website by this way may leads to stop sub-domains. I’d like to know an alternative way that make .htaccess to do that for www only not any thing before the first dot!
Some suggestions to achieve what you like, is written at the comments at this page: http://www.catswhocode.com/blog/10-useful-htaccess-snippets-to-have-in-your-toolbox
am a newbie here
could u explain what special characters in .htaccess files mean… in the above example
Tnx for code, just looking for it. It works in my case but with strange effect.
I have two domains, one is parked. If I put non-www code for both domains I get “redirect loop”. If I put code for just one of them it works – but second one is redirected to first domain. Probably it is normal for parked domains but I did not know and was surprised,, and it was just what I wonted :)))
I use the following on my site
Christopher Burton your code works perfect for me but how do I rewrite it to force www? This is doing my head in! Thanks.
@Andy
Have you tried something like this?
Thanks Christopher :-)
I was using the code at the top for a while with no bother, but just added a subdomain which it redirected.
So far it appears that your code is only redirecting attempts at the www subdomain, and is letting through all others.
Perfect! Long may it continue (browser (un)caching and/or general weirdness may kick in at any time after all)!
Any tips for where best to find details about the .htaccess language?
I can’t find anything very helpful at apache.org, but then I may be looking at the wrong pages.
Any pointers (to save constantly having to look for pre-constructed one-time solutions (like yours above)) would be appreciated.
Hey Christopher,
if I try yours, URLs of subpages get re-directed to the home page. Any idea why?
So: https://mydmain.com/mypage/ goes back to https://mydmain.com/index.php
Cheers,
/marc
@Christopher
Got it to work in my crude way, see line 5&6 below:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.
RewriteRule .* http://www.mysite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php
The second part of the mod_rewrite is for the swfaddress system I use which changes http://www.mysite.com/contact to http://www.mysite.com/#/contact which allows my Flash site to be indexed by search engines etc.
The problem is REQUEST_URI doesn’t recongnise the # symbol so if someone tries to access mysite.com/#/contact, rather than http://www.mysite.com/#/contact they just get http://www.mysite.com. If they try mysite.com/contact they will get http://www.mysite.com/#/contact.
This is the best I’m going to achieve I think but I do appreciate your help, thanks.
To build upon black-light-studio’s and Christopher Burton’s solutions. This will remove any number of www. subdomains. I don’t expect this to ever really happen, but what with the http://www.www.extra-www.org initiative you never know what some crazy server administrator might do.
This does not work in https://www.subdomain.domain.com
Apache recommends not using a RewriteRule for this sort of behavior. Rewrite processing can introduce more overhead to your website. You don’t want that rewrite rule to run over and over for valid URLs.
http://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost
Thanks! Works fine.
Cheers
Mat
this is work.. thanks alot..
i want ask something about .htaccess, and i have problem for this..
I have website and i want category be subdomain, like this, http://domain.com/category -> http://category.domain.com
can somebody help me for my problem.. thank you…
Simply a thing: it requires that Apache has enabled the rewrite module.
Perfect… working.. thank you so much… this is so great..
Hi,
I am working on static html website & now i want to hide “.html” extension Please give a any solution ASAP.
Thanks
Works Perfectly! Thanks
I can remove the .html extension using following code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
following this link
http://www.dailycreativedesign.com/remove-file-extension-from-url/
I’ve seen a million posts on how to add/remove the “www”. Likewise, I’ve seen a million posts on how to force http/https connections. How do you do both? I’m currently stuck: I can force https, and I can remove “www”, except when it’s combined. Example: https://www.example.com stays the same.
I am also looking for a solution to redirect http/https and remove the www. Would love to hear if anyone has a solution.
This will be a really easy question for this forum I am sure. I am not a web designer. Google originally indexed my parked domain (when on GoDaddy) as a non-www. It’s live now. And I submitted to google as www for site map and fetching. And it the site pages have been indexed and showing when I search with www. But when I search in Google for it as a non-www the parked domain information appears for my website. How can I reconcile/ merge/ remove the first non-www google had originally indexed with my live www website.
Thank you!
Great job, There are another tutorial about .htaccess, it provides some useful examples, also an example about forcing www http://www.bewebdeveloper.com/tutorial-about-advanced-htaccess-examples
If you are not using .htaccess then try like this
http://www.stuckngo.com/2014/11/how-to-add-custom-domain-on-my-blogspot.html
Hey! Thanks this is awesome! I needed this for my companies website, you are the best!
Thank you! : ))