WordPress.org

Ready to get started?Download WordPress

Forums

Error using .htaccess redirect to remove www (8 posts)

  1. yarnboy
    Member
    Posted 4 years ago #

    I have a client whose URL automatically appears with a "www" and she would like it removed. There is no .htaccess file, so I added one with the usual method for removing the www:

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

    When I do this, reloading the page results in an error and the page will not load. I have a feeling the above fix is disagreeing with something in WordPress, but I do not know what. Any help is greatly appreciated.

  2. Jonas Grumby
    Member
    Posted 4 years ago #

    I hope you know that you have to change domain.com to the actual domain name and extension that your site uses.

  3. yarnboy
    Member
    Posted 4 years ago #

    Yes, I know that. Thanks for checking, though. That was the result of a quick cut-and-paste.

  4. Jonas Grumby
    Member
    Posted 4 years ago #

    Try this. Again make sure your info is in there...

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

  5. Jonas Grumby
    Member
    Posted 4 years ago #

    Or this one:

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

    I'm just copying these from other people's examples. I am not testing them. You can find lots of examples here.

  6. Jonas Grumby
    Member
    Posted 4 years ago #

    BTW, I'm surprised that you don't have a .htaccess file in your WordPress folder. The code that WP generates will also remove the www. If WordPress is installed in the root folder, .htaccess should contain these lines:

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

    and if it is in a sub-folder named /blog it should look like this:

    # 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
  7. yarnboy
    Member
    Posted 4 years ago #

    Thanks, SS_Minnow. I'll give those a try. I was also surprised to find no .htaccess file. This installation of WP was a one-click install with Host Monster. Wonder if that has anything to do with it, since every other installation I've done has had the file.

  8. Jonas Grumby
    Member
    Posted 4 years ago #

    Remember that the last two that I posted should be in your WordPress folder, which is not necessarily the root. If WordPress is in a subfolder, you can use this in the root to redirect all requests from the root to the subfolder. Again I'll use /blog for the example:

    RewriteEngine On
    Options +FollowSymlinks
    RewriteCond %{HTTP_HOST} domain.ext$
    RewriteCond %{REQUEST_URI} !blog/
    RewriteRule ^(.*)$ blog/$1

    You can also make your domain name point to the sub-folder directly OR move the WordPress files to the root. In both cases you would have to update the database to recognize the new the URL. Information on how to do that is here.

    Keep in mind that if you are changing the path so that WordPress is no longer in a sub-folder you should take the references to the sub-folder name out of the .htaccess file as well.

Topic Closed

This topic has been closed to new replies.

About this Topic