WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] .htaccess Rewrite Engine (5 posts)

  1. pluto198
    Member
    Posted 7 years ago #

    Prior to installing WordPress, I had my .htaccess file set to redirect http://www.macbasement.com/* to http://macbasement.com/* (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} ^www.macbasement.com$
    RewriteRule (.*) http://macbasement.com/$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]
    </IfModule>
    # END WordPress

    Now, "www.macbasement.com" returns a "page not found" error in my browser, while "macbasement.com" (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.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Change your own rules to this:
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.macbasement.com$
    RewriteRule (.*) http://macbasement.com/$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.

  3. pluto198
    Member
    Posted 7 years ago #

    Perfect! Thank you!

  4. vkaryl
    Member
    Posted 7 years ago #

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

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

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

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

    Differences:
    - 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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags