2017-01-09 39 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]