Support » Fixing WordPress » How to redirect from www.domain.com to domain.com

  • If you have a WordPress blog installed in you root directory and you want all visitors who come to http://www.dmoain.com to be automatically redirected to domain.com (without the www prefix) then here is the solution. Using this solution will also keep your current directory sturucture, so if a visitor requests http://www.domain.com/dir/ he will be redirected to domain.com/dir/.

    Your current .htaccess in your root web directory should look like this before you start:

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

    After modification, this is how it looks:

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

    We only added the following two lines (a 301 redirect):

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

    I hope this helps

Viewing 11 replies - 1 through 11 (of 11 total)
  • Tarasque

    (@tarasque)

    I get an internal error whenever I try this 🙁

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Actually, it’s better to put this at the top of the file, above and outside all the WordPress stuff:

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

    That will do your redirection correctly. Change example.com to your own domain, of course.

    The example given by myahya above redirects everything that is not domain.com to domain.com. My example redirects *only* http://www.whatever&#8230; You may prefer this instead of redirecting everything.

    Tarasque

    (@tarasque)

    How would it work if I want to change non-www to http://www.whatever.com?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    How would it work if I want to change non-www to http://www.whatever.com?

    I can’t imagine why anybody would ever want to do that, but…

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

    BTW: www is deprecated.

    Tarasque

    (@tarasque)

    I tried the one that takes out the WWW, ended up again with the same error

    Internal Server Error
    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, webmaster@frostbolt.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

    I’m reverting the changes, but this is what I did

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.frostbolt\.com$ [NC]
    RewriteRule ^(.*)$ http://frostbolt.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

    It happened after I tried to see if the www would redirect to non-www.

    Tarasque

    (@tarasque)

    –Edited–

    I’m not sure if I should post what my .htaccess file has? Would it be helpful? Or it’s risky to post it? (Not sure if someone could do something against me by having what’s inside?)

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Unless it has something really weird in it, it’s fairly safe to post it.

    Anyway, the one you gave above should work. If that gives a server error, then something is wrong with your server. Talk to your host about it.

    Also, does the non-www work in the first place? You can’t redirect to it if it don’t work.

    Tarasque

    (@tarasque)

    Yes, the non-www work after changing the .htaccess.

    Tarasque

    (@tarasque)

    Ok, this is what my .htaccess file contains, the code I added was before # BEGIN WordPress

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    Options All
    
    <Files 403.shtml>
    order allow,deny
    allow from all
    </Files>
    
    deny from 74.52.109.18
    
    deny from 206.40.111.173
    deny from 206.40.111.168
    
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://elitistjerks.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://elitistjerks.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://fcatarasque.livejournal.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://fcatarasque.livejournal.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://feeds.feedburner.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://feeds.feedburner.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://forums.worldofwarcraft.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://forums.worldofwarcraft.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://frostbolt.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://frostbolt.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://pulse.guildvsguild.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://pulse.guildvsguild.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://worldofwarcraft.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://worldofwarcraft.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://wowace.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://wowace.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.feedburner.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.feedburner.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.frostbolt.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.frostbolt.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.google.com/reader/view/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.google.com/reader/view$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.pulse.guildvsguild.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.pulse.guildvsguild.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.wowace.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.wowace.com$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.elitistjerks.com/.*$      [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.elitistjerks.com$      [NC]
    RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ /index.php/index.php/index.php [R,NC]
    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    All the rest of the file is irrelevant. See this bit in the code I gave you?
    [R=301,L]

    That tells it to:
    a) Send a redirect and
    b) Stop processing (L = Last rule).

    Beyond that point, it doesn’t make any difference what’s in the .htaccess file.

    If you put in that code and get a 500 error, then something is wrong with your server. Period. End of discussion. We likely cannot help you anymore. Talk to your host, find out why their server is broken. Hosts usually understand .htaccess, they can probably help.

    Tarasque

    (@tarasque)

    My brother fixed it for me.

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

    The same as you suggested, but he said he used it without

    <IfModule>...</IfModule>

    Tags 😀 Thanks ^^

    My brother also just found out this, btw:

    http://photomatt.net/2006/06/13/wordpress-no-www/

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to redirect from www.domain.com to domain.com’ is closed to new replies.