将www重定向到非www

时间:2015-05-09 22:40:53

标签: apache .htaccess mod-rewrite redirect

我正在尝试将www网址重定向到我的论坛的非www。我的论坛安装在一个名为“forum”的子目录中。在我的根目录中,我安装了WordPress。对于我的根目录中的WordPress,.htaccess重定向工作正常,但我无法让它为我的论坛工作。

我尝试了一些我在谷歌搜索中找到的规则,但它们没有用。

我还检查了这个主题Generic htaccess redirect www to non-www

但在我的情况下,htaccess文件位于子目录中而不是根目录中。

可能我做错了所以这是我的整个.htaccess文件:

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

感谢任何帮助。 感谢

2 个答案:

答案 0 :(得分:1)

将此代码放在.htaccess文件中:

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

答案 1 :(得分:1)

您需要将www删除规则替换为以下内容:

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

确保在新浏览器中对此进行测试,以避免旧缓存。