Apache redirect www.domain.com to domain.com

Q. How do I remove www from all my urls so that search engine can only rank domain.com/ urls under Linux Apache web server?

A. Add following code to your .htaccess or httpd.conf (directory directive) file:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

If you added directives to httpd.conf, restart Apache:
# service httpd restart

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Igor Jun 3, 2008 @ 12:05


    can someone pls write me how to have primary apache server through https which will send requests to some local apache through http.

    Cheers, Igor

  • MTBVulture Dec 22, 2008 @ 22:16

    I’m in a “extremely incompetent hosting company” situation :) that don’t permit me to use my website without DNS.
    Now I’ve put a webserver APACHE on my PC always on with static IP and I’ve forwarded the DNS * and @ to my IP to get the request without www for my website and “redraw” them with WWW.
    I have some problems in doing this.
    For now I’ve tried this:

    $server = $_SERVER[‘SERVER_NAME’]; header(‘Location: http://www.’ . $server);

    but doesen’t work with every page (i.e. http://website.ext/* for every possible string in *).
    Can you suggest me how to fix this problem?
    I repeat, I have an apache webserver so I can also modify settings in apache if is not possible to do this only with PHP.
    Thanks so much.
    PS: It can be better if this fix works independent by domain name, so I can help people with same problem.
    For knowing, the hosting company is TopHost.it
    I remember you that my website is hosted OUTSIDE my pc! SO I need only a REDIRECT

  • Steve Dalton Jan 31, 2010 @ 11:33

    This worked for me, but after the redirect I get an extra slash on the end of the URL. eg.

    http://www.domain.com -> http://domain.com//
    http://www.domain.com/apage.html -> http://domain.com//apage.html

    That works fine – but looks odd.

    Also – any hints on getting this going for https (as well as http) too?

    ie. https://www.domain.com -> https://domain.com

  • foo Mar 29, 2010 @ 12:55

    Steve: just remove the / before $1

  • Stefan Jan 14, 2012 @ 16:41

    Hey there. I just created this script. it is independent of domain (will work on any domain)

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum