Php 从www重定向到非www不工作

Php 从www重定向到非www不工作,php,apache,.htaccess,redirect,vhosts,Php,Apache,.htaccess,Redirect,Vhosts,我有一个网站上运行的工艺CMS,我不知道如果问题是直接与CMS或服务器 我正在使用.htaccess将任何www url重写为非www版本 <IfModule mod_rewrite.c> # (1) RewriteEngine On # (2) Options +FollowSymlinks # (3) #Options +SymLinksIfOwnerMatch # (4) #RewriteBase /

我有一个网站上运行的工艺CMS,我不知道如果问题是直接与CMS或服务器

我正在使用.htaccess将任何www url重写为非www版本

<IfModule mod_rewrite.c>

    # (1)
    RewriteEngine On

    # (2)
    Options +FollowSymlinks

    # (3)
    #Options +SymLinksIfOwnerMatch

    # (4)
    #RewriteBase /

    # (5)
    #RewriteOptions <options>

    # (6)
    RewriteCond %{HTTPS} =on
    RewriteRule ^ - [env=proto:https]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ - [env=proto:http]


    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
#   RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>
这是vhost文件的名称

<VirtualHost *:80>
    ServerName domain.com
    ServerAdmin webmaster@domain.com
    DocumentRoot /var/www/

    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

ServerName域名.com
服务器管理员webmaster@domain.com
DocumentRoot/var/www/
选项索引跟随符号链接多视图
允许超越所有
命令允许,拒绝
通融
ErrorLog${APACHE_LOG_DIR}/error.LOG
CustomLog${APACHE\u LOG\u DIR}/access.LOG组合
我已经看过了,但是没有任何变化

我使用的是Ubuntu 16.04.1、Apache 2.4.18和7.0.8-0ubuntu0.16.04.3 托管在Ubuntu上


有什么我可以测试的,看看我是否可以让www重定向到非www正常工作吗?

您需要在其他表达式之前移动此块:

<IfModule mod_rewrite.c>
    RewriteEngine On
#   RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>
强迫它

RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]

太棒了,谢谢你,真管用!我将重新组织我的htaccess文件。
<IfModule mod_rewrite.c>

    # (1)
    RewriteEngine On

    # (2)
    Options +FollowSymlinks

    # (3)
    #Options +SymLinksIfOwnerMatch

    # (4)
    #RewriteBase /

    # (5)
    #RewriteOptions <options>

    # (6)
    RewriteCond %{HTTPS} =on
    RewriteRule ^ - [env=proto:https]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ - [env=proto:http]

#   RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

</IfModule>
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]