69

This seems to be a non-issue for many people (read: I can't find an answer), but I would like to update the following htaccess code to not only remove the 'www' from the URL, but also any sub-directories that are accessed.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

With this, http://www.example.com/any/ resolves fine, but I want it to redirect to http://example.com/any/ as with the root.

Cycododge
  • 943
  • 1
  • 8
  • 10

5 Answers5

170

I had the same problem (trouble stripping 'www' from URLs that point to a sub-directory on an add-on domain), but after some trial and error, this seems to work for me:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
Black
  • 18,150
  • 39
  • 158
  • 271
phpmonkey
  • 1,816
  • 1
  • 12
  • 5
  • 1
    This also works for the same link without 'prettifying' the URL, e.g. [http://www.addondomain.com/projects/a/index.php?where=gallery&cid=4&id=2](http://www.addondomain.com/projects/a/index.php?where=gallery&cid=4&id=2) -> [http://addondomain.com/projects/a/index.php?where=gallery&cid=4&id=2](http://addondomain.com/projects/a/index.php?where=gallery&cid=4&id=2) – phpmonkey Jan 02 '12 at 09:39
  • 2
    Good answer - this works generically without any editing... just drop it in to your .htaccess file and you're awway. – Doug May 22 '13 at 09:00
  • 2
    This is currently the best answer to removing www from your host address because you don't hard code a domain name so you can easily push your changes to your remote server without a need to modify .htaccess . Thanks for your answer. – Iman Mohamadi Mar 12 '14 at 14:16
  • worked perfect. note: it most be after "RewriteEngine on" exactly :) – Mansour Alnasser Mar 20 '14 at 07:04
  • 5
    What if user enters `https://www.domain.com/log-me-in`? He will be redirected to `http`. So I need another rewrite from `http` to `https` right? – TheFrost Apr 30 '14 at 09:01
  • This must add before `RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d` – Preslav Panayotov Aug 04 '14 at 00:13
  • Works like a charm :) Completly removed the www for addon domains and main domain. – 2Noob2Good Jan 23 '15 at 16:27
  • 2
    @TheFrost Yes you do, `RewriteCond %{HTTPS} off` `RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]` – DanielTheGeek Sep 24 '16 at 02:28
  • Worked for me Thanks – SAYE Dec 28 '18 at 22:18
37

Redirection code for both non-www => www and opposite www => non-www. No hardcoding domains and schemes in .htaccess file. So origin domain and http/https version will be preserved.

APACHE 2.4 AND NEWER

NON-WWW => WWW:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WWW => NON-WWW:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]

Note: not working on Apache 2.2 where %{REQUEST_SCHEME} is not available. For compatibility with Apache 2.2 use code below or replace %{REQUEST_SCHEME} with fixed http/https.


APACHE 2.2 AND NEWER

NON-WWW => WWW:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

... or shorter version ...

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|offs
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WWW => NON-WWW:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

... shorter version not possible because %N is available only from last RewriteCond ...

jQuerybeast
  • 14,130
  • 38
  • 118
  • 196
mikep
  • 5,880
  • 2
  • 30
  • 37
  • +1 for version options.. Question: shouldn't the NE flag be used? .. it'd be fine without it if you assume this will only 'hit' when the url is typed manually, but what about any outdated 'www.' links 'in the wild'. I just found one such link forgotten in an ancient email template :P – DM8 Mar 09 '18 at 20:00
  • 1
    @DM8 No, `NE` flag (noescape) should not be used in this case. URL `example.com/?a=1&b=2` is correctly redirected to e.g. `www.example.com/?a=1&b=2`. That flag should be used when you have special chars in URL in .htaccess to redirect to e.g. `RewriteRule "^/anchor/(.+)" "/bigpage.html#$1" [NE,R]` (# will not be escaped on redirect thanks to NE flag). – mikep Mar 12 '18 at 07:34
  • Great resource, @mikep. One note, in my case, I had to use %{ENV:HTTPS} and not %{HTTPS}. – typeoneerror Jul 03 '18 at 18:12
  • @typeoneerror yes, it depends on situation, but standard variable in apache-only configuration is %{HTTPS} but when you use something custom like `SetEnvIf X-HTTPS on HTTPS=on` (X-HTTPS comes from proxy before apache such as nginx) then you must use mentioned %{ENV:HTTPS} – mikep Jul 04 '18 at 07:49
13

I think you're close, but try the following:

# force non-www domain
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

Not sure exactly what you mean about sub-directories, but this follows your example.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • My apologies, I meant that [http://www.example.com](http://www.example.com) and [http://www.example.com/any/](http://www.example.com/any/) should be redirected to [http://example.com](http://example.com) and [http://example.com/any/](http://example.com/any/) respectively. The current setup only allows the root to redirect. – Cycododge Jun 29 '11 at 16:23
  • I just tried it another domain, and it does work. I'm curious why then it isn't working on the one I am trying. Thanks for replying anyways. *Now I know why it is a non-issue for most people. – Cycododge Jun 29 '11 at 17:39
  • I take that back, it does not work. Example [http://www.avdra.com/00.districts/06/index.php](http://www.avdra.com/00.districts/06/index.php). It does work however on [http://www.avdra.com](http://www.avdra.com). – Cycododge Jun 29 '11 at 17:41
  • I think I just figured out the issue. This htaccess code does not work on addon domains for URLs that point to a sub-directory. – Cycododge Jun 29 '11 at 17:54
  • This was a HostGator issue for anybody having the same problem. They had to solve it on their end. – Cycododge Sep 02 '11 at 21:44
0

I use this code. If my visitor does not have www in his url then this condition adds www with url, otherwise no need to add www with url ( because he already has. :) )

RewriteEngine On
RewriteCond %{HTTP_HOST}  !^www\.YOUR-SITE\.com$ [NC]
RewriteRule ^(.*) http://www.YOUR-SITE.com/$1 [L,R]
Adnan Ahmad
  • 848
  • 1
  • 13
  • 12
-4

Hello, the code works perfectly, except that it passes with the www in a url with some value and slash at the end it shows the parameter and value in the url.

RewriteEngine On SetOutputFilter DEFLATE AddOutputFilterByType DEFLATE text/html text/plain text/xml RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(!.(\.gif|\.jpg|\.png|\.swf|\.css|\.js|\.txt|\.php|\.htm|\.html)|.+[^/])$ /$1/ [R=301,NC] RewriteRule ^(.[^.*]+)\/$ ?jogar=$1 [NC] Options -Indexes Options +FollowSymLinks RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http:\/\/%1%{REQUEST_URI} [R=301,QSA,NC,L]