2017-01-09 87 views
0

我有一个网站在Craft CMS上运行,我不确定问题是否直接与CMS或服务器有关。从www重定向到非www不工作

我使用的.htaccess当我键入domain.com/about它工作正常,但是当我键入www.domain.com/about它改变了网址domain.com/index.php?p=about

重写任何WWW网址到非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> 

在Craft配置中我启用了'omitScriptNameInUrls' => true,,它与下面一起应该从URL中删除index.php。

RewriteEngine On 

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

这是vhost文件。

<VirtualHost *:80> 
    ServerName domain.com 
    ServerAdmin [email protected] 
    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> 

我已经浏览了https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost,但没有任何改变。

我使用Ubuntu 16.04.1,Apache 2.4.18和7.0.8-0ubuntu0.16.04.3 托管在Ubuntu上。

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

回答

1

您需要的其他表达式之前移动块:虽然你已经在上一次的L标签

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

,所以应该最后应用的规则,这是行不通的(可能是因为您稍后再次激活RewriteEngine以便重置规则,但是如果L标志在第一条规则中处于活动状态,则它不会应用www)。

经验法则是:首先对域进行整体修改,然后应用功能规则。你也不需要打开2 IfModule条件。结合二合一为此:

<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> 
+0

太棒了,谢谢你的工作!我将重新组织我的htaccess文件。 – zizther

0

强制它!

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