Support » Fixing WordPress » Remove www using .htaccess

  • I’d like to canonicalize my URLs by removing the leading “www”. I’ve managed to do this site-wide using a .htaccess in the root web directory (/home/me/example.com/). This reads:

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

    It’s working everywhere except in the directory containing my WordPress installation (/home/me/example.com/blog/), presumably because WordPress has already created a .htaccess in that folder, which reads:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /blog/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]
    </IfModule>
    # END WordPress

    So I tried combining my .htaccess and WordPress’s, and came up with:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /blog/
    RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/blog/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]
    </IfModule>
    # END WordPress

    This works for almost everything:

    http://www.example.com/blog/ > http://example.com/blog/
    http://www.example.com/blog/archives/69 > http://example.com/blog/archives/69

    and URLs without the leading “www” are untouched. Great! Except, one slight problem:

    http://www.example.com/blog > http://example.com/blog//home/me/example.com/blog

    (note absence of trailing slash on pre-rewritten URL.) So, I feel like I’m nearly there, but I’ve been futzing around with this file for hours and have been unable to figure this out. Does anyone have any ideas?

Viewing 5 replies - 1 through 5 (of 5 total)
  • I’ve had the best luck with root folder wp installs putting the not-wp stuff (like the no-www rewrites) below the wp lines, in a separate <if module> section. Even then it seems to not always work, and I’ve finally resorted to having my host provider change all my domains to no-www ServerName parameter. This is done through the apache configuration on their end.

    AskApache

    (@cduke250)

    Hey caek00 this should work for ya!

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    
    RewriteCond %{REQUEST_URI} ^/?blog.* [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]

    From Ultimate Apache htaccess Article

    My wordpress is installed on the root directory. So i tried writing the script like this:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.yourdomain.com$ [NC]
    RewriteRule ^(.*)$ http://txtbuff.com/$1 [R=301,L]
    
    RewriteCond %{REQUEST_URI} ^/?.* [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    But the search function is not working well. How do I fix this?

    I had my site set up like that with this in my .htaccess:

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

    However, it caused me problems later with a few plugins. One of the plugins was podPress. I can’t remember the other ones that had problems.

    Anyway, I then tried two different plugins that also got rid of the WWW, but they also had problems with the aforementioned ones. Consequently, I gave up on the whole notion, because I needed to use those plugins.

    I just wanted to make you aware that there could be problems later on down the line with plugins not working, if you do this.

    Hello. I seem to have a solution built from the responses above that is working without issue on my site. I hope this helps someone.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Remove www using .htaccess’ is closed to new replies.