.htaccess permanent redirect to www

I have a wordpress instalation that is not redirecting urls not starting with www.

Example: http://example.com/dir/ doesn’t send to http://www.example.com/dir/ instead goes to http://example.com/

Read More

How do I change the .htaccess below to always redirect to the page either using www or not?

# BEGIN WordPress
RewriteEngine On

RewriteBase /
RewriteRule ^index.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
RewriteRule . index.php [L]

# END WordPress

Related posts

Leave a Reply

4 comments

  1. You should not have to modify your .htaccess; actually I would not recommend it.

    Sounds like you simply have your values for WordPress address and Site address set correctly in your admin console in the Settings > General section:

    General section and the WordPress address and Site address”>

    Another option is to add the following 2 lines somewhere within your /wp-config.php file but before require_once(ABSPATH . 'wp-settings.php'); (and be sure not to use a trailing slash):

    define('WP_HOME',"http://www.example.com/dev");
    define('WP_SITEURL',"http://www.example.com/dev");
    

    I prefer this approach because I like to keep my configuration options in code whenever possible since it is easier to maintain that way than configuration options stored in the database.

    Hope this helps.

    -Mike

    P.S. Next time you have a WordPress question, consider asking over at StackOverflow’s sister site WordPress Answers? Lots of WordPress enthusiasts over the to help out.

  2. I got the correct answer from webmasters.stackexchange.com. This works perfectly! Thanks Ruel for pointing me to webmasters.stackexchange.

    Options +FollowSymlinks
    RewriteCond %{HTTP_HOST} !^(www.|$) [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
  3. More like a question for http://webmasters.stackexchange.com

    Well anyway, if you are using cPanel, you can do a wildcard redirect from Redirects under Domains category.

    But if not, try this:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com[nc]
    RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
    
  4. Works perfectly. Put this at the top of wp-config.php file:

    define('WP_HOME',"http://www.example.com/dev");
    define('WP_SITEURL',"http://www.example.com/dev");