繁体   English   中英

从www重定向到非www无效

[英]Redirect from www to non-www not working

我有一个在Craft CMS上运行的网站,不确定该问题是否直接与CMS或服务器有关。

我正在使用.htaccess将任何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>

当我键入domain.com/about它工作正常,但是当我键入www.domain.com/about它将URL更改为domain.com/index.php?p=about

在Craft配置中'omitScriptNameInUrls' => true,启用了'omitScriptNameInUrls' => true, ,将其与以下内容一起从网址中删除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 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>

我已经浏览了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?

您需要将该块移动到其他表达式之前:

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

尽管您在前一个标记中具有L标记,所以它应该是最后一个应用的规则,但它不起作用(可能是因为稍后您再次激活RewriteEngine从而重置了该规则,但是如果L标记在第一个标记中处于活动状态规则,则无论如何都不会应用www )。

经验法则是:首先对域进行修饰,然后应用功能规则。

您也不需要打开2个IfModule条件。 将2合为一个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM