Support » Fixing WordPress » .htaccess Rewrite Engine

  • Resolved pluto198


    Prior to installing WordPress, I had my .htaccess file set to redirect* to* (basically removes the ‘www’ prefix, because I find it unnecessary) I had this working with the following code in my .htaccess:
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^$
    RewriteRule (.*)$1 [R=Permanent]

    (Courtesy of John Gruber)

    When WordPress was installed, it modified my .htaccess file, adding the following code after my code:
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress

    Now, “” returns a “page not found” error in my browser, while “” (sans ‘www’) works fine (WordPress permalinks and all)

    I’m guessing there’s a certain way to combine two Rewrite rules, which I am not familiar with. Because separately, both codes work fine, but when combined they don’t work right together.

    Could someone take the above two htaccess codes and combine them so they will work properly?

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Change your own rules to this:
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^$
    RewriteRule (.*)$1 [R=301,L]

    The 301 is the same as “Permanent” (but more sensible since it actually tells you what the code being sent back is), and the L forces it to stop processing rewrites at that point and redirect the user immediately to the non-www URL.

    Perfect! Thank you!

    Yes, it is! Thanks, Otto, something I didn’t know I needed has now been magically provided….

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    BTW, while the above *works*, the better way is as follows:

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

    – The dots in the domain name are escaped, thus making them actually match dots and not any single character.
    – The [NC] makes it case-insensitive.
    – The ^ and $ enclosing the rule make it a bit more intelligently match the entire URL when doing the rewrite.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘.htaccess Rewrite Engine’ is closed to new replies.