从 www 重定向到非 www 不工作
Redirect from www to non-www not working
我在 Craft 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= [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,
,连同下面的设置应该从 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= [QSA,L]
这是虚拟主机文件。
<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
个条件。合二为一:
<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= [QSA,L]
</IfModule>
给力!
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://domain.com/ [R,L]
我在 Craft 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= [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,
,连同下面的设置应该从 URL.
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= [QSA,L]
这是虚拟主机文件。
<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
个条件。合二为一:
<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= [QSA,L]
</IfModule>
给力!
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://domain.com/ [R,L]