Xenforo and Wordpress friendly force WWW in URL?

On one of my XF sites I have wordpress installed on the root directory and xenforo installed in the /forums folder. What I’d like to do is to force all non-www traffic to include the www prefix. Right now it only kind of works, if I go to MYDOMAIN.com it redirects to www.MYDOMAIN.com, so far so good.

But if I go to MYDOMAIN.com/forum it redirects me to the main page, which I can then click on the forum link in the menu (www included) and the forum loads. However I’m not able to just type MYDOMAIN.com/forum and it redirects to www.MYDOMAIN.com/forum

Here is the root .htaccess file

<FILES .htaccess>
order allow,deny
deny from all
</FILES>
##
#         FORCE NON-WWW to WWW URLS
RewriteEngine On
RewriteCond %{HTTP_HOST} ^MYDOMAIN.com
RewriteRule (.*) http://www.MYDOMAIN.com/$1 [R=301,L]
#         DISALLOW DIRECTORY BROWSING
Options ALL -Indexes
##
#         ENABLE GZIP COMPRESSION
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</IfModule>
##
#        EXPIRES CACHING
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
##
#        PHPBAY START
<IfModule mod_rewrite.c>
# this goes above ANY Wordpress htaccess code in the .htaccess file
# if your site is installed in a folder, ie: http://www.yoursite.com/folder/
# add the folder to name to the RewriteBase line, ie: RewriteBase /folder/
RewriteBase /
RewriteRule ^media/1/images/e/(.*)$ http://thumbs.ebaystatic.com/pict/$1 [R,L]
RewriteRule ^media/1/images/f/(.*)$ wp-content/plugins/phpBay/media/images/$1 [R,L]
RewriteRule ^media/1/css/(.*)$ wp-content/plugins/phpBay/media/css/$1 [R,L]
RewriteRule ^watch-list-(.*)_(.*)_(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&campaignid=$5&linktype=watch&mode=debug [R,L]
RewriteRule ^watch-list-(.*)_(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&linktype=watch&mode=debug [R,L]
RewriteRule ^watch-list-(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&linktype=watch&mode=debug [R,L]
RewriteRule ^watch-list-(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&linktype=watch&mode=debug [R,L]
RewriteRule ^watch-list-(.*)_(.*)_(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&campaignid=$5&linktype=watch [R,L]
RewriteRule ^watch-list-(.*)_(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&linktype=watch [R,L]
RewriteRule ^watch-list-(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&linktype=watch [R,L]
RewriteRule ^watch-list-(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&linktype=watch [R,L]
RewriteRule ^bid-(.*)_(.*)_(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&campaignid=$5&mode=debug [R,L]
RewriteRule ^bid-(.*)_(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&mode=debug [R,L]
RewriteRule ^bid-(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&mode=debug [R,L]
RewriteRule ^bid-(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&mode=debug [R,L]
RewriteRule ^bid-(.*)_(.*)_(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&campaignid=$5 [R,L]
RewriteRule ^bid-(.*)_(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4 [R,L]
RewriteRule ^bid-(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3 [R,L]
RewriteRule ^bid-(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2 [R,L]
RewriteRule ^bin-(.*)_(.*)_(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&campaignid=$5&mode=debug [R,L]
RewriteRule ^bin-(.*)_(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&mode=debug [R,L]
RewriteRule ^bin-(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&mode=debug [R,L]
RewriteRule ^bin-(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&mode=debug [R,L]
RewriteRule ^bin-(.*)_(.*)_(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&campaignid=$5 [R,L]
RewriteRule ^bin-(.*)_(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4 [R,L]
RewriteRule ^bin-(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3 [R,L]
RewriteRule ^bin-(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2 [R,L]
RewriteRule ^item-(.*)_(.*)_(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&campaignid=$5&mode=debug [R,L]
RewriteRule ^item-(.*)_(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&mode=debug [R,L]
RewriteRule ^item-(.*)_(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&mode=debug [R,L]
RewriteRule ^item-(.*)_(.*)_debug.html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&mode=debug [R,L]
RewriteRule ^item-(.*)_(.*)_(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4&campaignid=$5 [R,L]
RewriteRule ^item-(.*)_(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3&ccid=$4 [R,L]
RewriteRule ^item-(.*)_(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2&country=$3 [R,L]
RewriteRule ^item-(.*)_(.*).html$ wp-content/plugins/phpBay/auction.php?title=$1&item=$2 [R,L]
</IfModule>
#        PHPBAY END
##
#        FILTER PHP SCRIPT INJECTIONS
#Options +FollowSymLinks
#RewriteCond %{QUERY_STRING} (\\<|%3C).*script.*(\\>|%3E) [NC,OR]
#RewriteCond %{QUERY_STRING} GLOBALS(=|\\[|\\%[0-9A-Z]{0,2}) [OR]
#RewriteCond %{QUERY_STRING} _REQUEST(=|\\[|\\%[0-9A-Z]{0,2})
#RewriteRule ^(.*)$ index.php [F,L]
##
#        WORDPRESS START
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#        WORDPRESS END

And this is the htaccess file from my Xenforo forum.

# PREVENT VIEWING OF .HTACCESS
<Files .htaccess>
    Order deny,allow
    Deny from all
</Files>
#
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^MYDOMAIN\\.com
RewriteRule (.*) http://www.MYDOMAIN.com/$1 [R=301,L]
</IfModule>
#
<IfModule mod_rewrite.c>
    # RewriteEngine On

    #    If you are having problems with the rewrite rules, remove the "#" from the
    #    line that begins "RewriteBase" below. You will also have to change the path
    #    of the rewrite to reflect the path to your XenForo installation.
    RewriteBase /forum

    #    This line may be needed to enable WebDAV editing with PHP as a CGI.
    #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(data/|js/|styles/|install/|favicon\\.ico|crossdomain\\.xml|robots\\.txt) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
</IfModule>

If I remove the redirect bit from my /forum htaccess file the forum is then accessible from either www or non-www.

Any suggestions would be much appreciated!

Michael,

Your code,

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

is sufficient although I would have done it slightly differently (my coding preference):

RewriteEngine on # Only required once to ensure you're not in comment mode
RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I used the negative of your ^MYDOMAIN\.com to match only if the domain does not start with www. which was your stated intention. Of course, escaping the dot character in your code AND using the No Case flag would be highly advisable. I used the {HTTP_HOST} variable which will work on any domain (which resolves to your DocumentRoot) and I ensure a match with .? as the RewriteRule’s regex. Finally, I prefer to use the Apache variable, {REQUEST_URI}, in the redirection so I don’t have to be concerned with any additional capture.

Placing either bit of code in your DocumentRoot will do the trick for you.

Too much information (all the other crud in your .htaccess) gets a critique:

First, the .htaccess file must be read and parsed multiple times for every request (including supporting files) so it MUST be kept to a minimum. If you’re not using a shared account (have access to the httpd.conf - or your host will modify it for you), then put your debris in there where it must only be read on Apache start.

  1. It’s good that you protect your .htaccess file(s).

  2. (Technique) I recommend separating your Apache directives (Options All -Indexes) at the top of your .htaccess file. Embedding in the module code is only confusing.

  3. I loathe using <IfModule> tests in .htaccess files! If you don’t know whether your server has the module enabled or not, you are not worthy of being a webmaster. Test ONCE by commenting out the <IfModule> then either remove the <IfModule> wrapper OR it and the code it contains. That applies to GZIP and CACHING as well as mod_rewrite.

[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/rant 4]

Nothing personal, it’s just code I had developed as a TL here to deal with this nonsense. Anyway, best to have it in the httpd.conf file.

  1. Your PHPBAY code looks like it’s over the top but not worth trying to combine statements. Not sure why you’d need all this but that wasn’t your question so it’s not worth pursuing.

  2. Filter PHP script injections? If it’s commented out, delete it from your production server!

  3. WP uses their <IfModule> to prevent newbies from crippling their website. This should NOT apply to you go get rid of this SECOND <IfModule> for mod_rewrite! Additionally, I dislike their RewriteBase (in the DocumentRoot where it’s not needed) as well as their first RewriteRule (which only duplicates the second RewriteRule (set with the RewriteCond statements).

  4. The above also applies to the Xenfro .htaccess EXCEPT, if this is in a subdirectory of the WP install, you’ll have to add a line in the DocumentRoot to exclude this directory from your WP redirect to its own index.php.

Sorry, I’ve repeated this information so many times over the years that I must appear edgy. Nothing personal; I hope the comments help (as well as the explanation of why my code is different than yours when forcing www.

If you need a tutorial (IMHO, it might help only on the finer points of mod_rewrite as you’ve not done a bad job despite my nagging), have a look through the lengthy tutorial article linked in my signature. It’s helped many members in the past and should help some of the explanations (and is likely more value to others so it’s worthy of mention here if only for that).

Regards,

DK

@dklynn I appreciate your very detailed reply. I’m certainly not a “webmaster” out of profession but out of necessity really so getting this kind of feedback does help me a lot. Learning more about this is really what I’d like to do, finding time is usually the hard thing. :wink:

I agree with you on the IfModules, they aren’t needed I know that. I have a tendency to reuse htaccess across many of my sites, some of which are not hosted on my server and just found it easier to leave it there. Bad practice I know. I’ve now deleted said bits of code.

The phpbay bit of code, I too believe it looks a bit hairy but being this is what the developer suggests and me not understanding this have stuck with what works.

With that I was able to get this redirect working. In my root .htaccess I cleaned up the code as suggested and left all else alone, including the non-www to www code. In my xenforo htaccess (located in /forum) I changed the non-www to www code to this:


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

What would be the benefit of doing the negative as you suggested above verses what I’m doing now?

WoodiE,

Thanks for not taking offense (where none was meant).

I suppose it’s okay to leave the <IfModule> wrapper in but it is a HORRIBLE waste of resources. Definitely remove them on your website (or after testing on a client’s - one that you care about).

The problems with our code,

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

were mentioned before (albeit it’s nice to see the dot character escaped). Remember, please that your code will match MYDOMAIN.com but NOT MyDoMaIn.CoM, i.e., the No Case flag is necessary because the {HTTP_HOST} is NOT case sensitive (while regex is unless the NC flag is used).

Adding the forum subdirectory is NOT a good thing to do as you surely have scripts in the DocumentRoot that you want to have visitors see (even if they don’t use a www’d domain). If the forum is in the {REQUEST_URI}, it’ll be there in the redirection. If you’d decided to move this code to the forum subdirectory, then it would be okay as is.

My reason for being negative (other than that apparently being my nature :smiley: ) is that it will redirect EVERYTHING which was not requested from the www’d domain without changing anything else in the request. To me, that makes the redirection easier for Apache and simpler for my “simple mind” to comprehend. As I said, it’s simply my preference, though (and I don’t have to think about creating new Apache variables, fiddling with query strings, POST is left alone, and there is no “leading slash” questions to contend with … in the early days of Apache 2).

Regards,

DK