我有一个网站,似乎不能从非www重定向到www。
我的Apache配置如下:
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
我错过了什么?
我有一个网站,似乎不能从非www重定向到www。
我的Apache配置如下:
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
我错过了什么?
当前回答
如果你有多个域名(可选)
-如果所有这些域名都使用https(因为他们应该)
-如果你想让所有的域名都使用www。domainName
这将避免doble重定向(http://non www到http://www,然后到https://www)
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ https://www.%1$1 [R=301,L]
</VirtualHost>
And
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
您应该将重定向代码301更改为最方便的代码
其他回答
我使用的代码是:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
我在WP Multisite上有一个类似的任务,其中重定向规则必须是通用的(对于我添加到网络中的任何给定域)。我首先解决了向域(停放的域)添加通配符。注意。com之后。
CNAME * domain.com.
然后我在我的多站点根目录下的.htaccess文件中添加了以下代码行。我想它适用于任何网站。
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
希望这能有所帮助。
ps.如果你想从not www重定向到www,把最后一行改为
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^!example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This starts with the HTTP_HOST variable, which contains just the domain name portion of the incoming URL (example.com). Assuming the domain name does not contain a www. and matches your domain name exactly, then the RewriteRule comes into play. The pattern ^(.*)$ will match everything in the REQUEST_URI, which is the resource requested in the HTTP request (foo/blah/index.html). It stores this in a back reference, which is then used to rewrite the URL with the new domain name (one that starts with www).
[NC]表示不区分大小写的模式匹配,[R=301]表示使用代码301进行外部重定向(资源永久移动),[L]停止所有进一步重写,并立即重定向。
如果你只想加载www的https版本,在apache虚拟主机文件中使用下面的配置。所有这些都可以放在一个文件中。
重定向所有HTTP到www的HTTPS:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
重定向HTTPS非www到HTTPS www:
<VirtualHost *:443>
ServerName example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
实服务器配置
<VirtualHost *:443>
ServerAdmin hostmaster@example.com
DocumentRoot "/path/to/your/sites/.htaccess-file-folder"
SetEnv APPLICATION_ENV "production"
<Directory "/path/to/your/sites/.htaccess-file-folder">
Options Indexes FollowSymLinks
DirectoryIndex index.php index.html
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ServerName www.example.com
SSLEngine ON
SSLCertificateFile "/path/to/your/example.cert.pem"
SSLCertificateKeyFile "/path/to/your/example.key.pem"
ErrorLog /path/to/your/example.com-error.log
CustomLog /path/to/your/example.com-access.log combined
#Your other configurations if you have
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "what/ever/root/to/source"
ServerName www.example.com
<Directory "what/ever/root/to/source">
Options FollowSymLinks MultiViews Includes ExecCGI
AllowOverride All
Order allow,deny
allow from all
<What Ever Rules You Need.>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
这就是上面代码所发生的情况。第一个虚拟主机块检查请求是否为www.example.com,并在该目录下运行您的网站。
如果做不到这一点,就进入第二个虚拟主机部分。在这里,除www.example.com之外的任何内容都被重定向到www.example.com。
这里的顺序很重要。如果你先添加第二个virtualhost指令,它将导致一个重定向循环。
此解决方案将重定向到您的域www.yourdomain.com的任何请求。
干杯!