Search code examples
wordpress.htaccesshttp-redirectwebsubdirectory

non-www to www redirect not working for articles in subdirectory installation of WordPress


I am helping out with a website that has a main/root website as well as a WordPress installation in a sub-directory. The .htaccess file of the main site has rules to redirect non-www traffic to www:

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

However, this has no effect on the WP site. For instance:

  • site.com would redirect to www.site.com
  • site.com/some-content-belonging-to-root-site would redirect to www.site.com/some-content...
  • site.com/wordpress does not redirect to www.site.com/wordpress

I added similar rules to the WordPress .htaccess, playing around to try to get the result I would expect, but the best I could come up with is this:

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

...which successfully redirects site.com/wordpress to www.site.com/wordpress, but does not redirect site.com/wordpress/a-blog-post.

I feel like I must be missing something simple or obvious, but hours of experimentation and research have not yielded a thing except that .htaccess files in sub-directories are not advised (but I think this is unavoidable in this case).

I would be grateful for any suggestions on how to fix the rewrite condition or rule to successfully redirect the post links of the WP site.

Thanks!

EDIT / ADDITIONAL INFO: I modified the RewriteCond to better match the incoming URLs, thinking that this might help, but it has the same effect. The base wordpress URL is rewritten, but article URLs are not:

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

I wouldn't think that it should matter, but just in case, I'll mention that the article URLs look like this: site.com/wordpress/2014/05/16/article-slug


Solution

  • It turned out that the problem was not the rewrite conditions or rules, but the order in which they appeared in the .htaccess file.

    I read a WP forum post in which someone suggested moving the rewrites to the top of the file for performance reasons. I gave it a shot just to see what would happen and noticed that the URL for the articles was affected - not in a good way, but affected nonetheless. I got something like this:

    www.site.com/wordpressindex.php?/2014/05/16/article-slug

    There were some rewrite rules to rewrite the base and redirect to the index given a nonexistent directory or file, which was what was building the garbled URL above.

    In case it happens to help anyone else, I began with a .htaccess file that looked like this:

    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /wordpress/index.php [L]
    </IfModule>
    # END WordPress
    

    And ended up rearranging it to look like this, which now works to properly redirect all blog-related URLs to the www site:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteBase /wordpress/
    
        RewriteCond %{HTTP_HOST}/wordpress ^site\.com/wordpress [NC]
        RewriteRule (.*) http://www\.site\.com/wordpress/$1 [R=301,L]
    
        RewriteCond %{SCRIPT_FILENAME} !-f
        RewriteCond %{SCRIPT_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [L]
    
        RewriteRule ^index\.php$ - [L]
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /wordpress/index.php [L]
    </IfModule>