1

I'm trying to redirect www URLs to non-www for my forum. My forum is installed in a subdirectory called "forum". In my root directory I've got WordPress installed. For the WordPress in my root directory the .htaccess redirection works fine, but I'm having trouble get it working for my forum.

I've tried a couple of rules that I found in Google search but they didn't work.

I've also checked this topic Generic htaccess redirect www to non-www

but in my case the htaccess file is in a subdirectory and not in the root.

Probably I'm doing something wrong so here is my entire .htaccess file:

#   Mod_security can interfere with uploading of content such as attachments. If you
#   cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#   SecFilterEngine Off
#   SecFilterScanPOST Off
#</IfModule>

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 405 default
ErrorDocument 406 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 503 default

<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
RewriteCond %{HTTP_HOST} www.example.com$
RewriteRule ^(.*)$ http://example.com/forum$1 [R=301,L]


#   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>

Any help is appreciated. Thanks

Community
  • 1
  • 1
Tom
  • 53
  • 1
  • 10
  • possible duplicate of [Generic htaccess redirect www to non-www](http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www) – DACrosby May 09 '15 at 22:56

2 Answers2

1

Place this code in your .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Llogari Casas
  • 942
  • 1
  • 13
  • 35
  • Thanks for the quick reply, but it doesn't work unfortunately. It gives me 404 errors. – Tom May 09 '15 at 22:53
  • 1
    Try to clean your entire .htacces file and place just this code. You had several redirects to www.example.com in your example. I've used this Rewrite Rule several times and it always worked for me @Tom – Llogari Casas May 09 '15 at 22:54
  • I am sorry to hear that, maybe you could try to use a range RegEx to achieve that. But not sure at all, sorry to be not helpful @Tom – Llogari Casas May 09 '15 at 23:03
1

You need to replace your www removal rule with this one:

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

Make sure to test this in a new browser to avoid old cache.

anubhava
  • 761,203
  • 64
  • 569
  • 643