我有一个网站,似乎不能从非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] 

我错过了什么?


当前回答

如果你正在使用Apache 2.4,不需要启用重写Apache模块,你可以使用这样的东西:

# non-www to www
<If "%{HTTP_HOST} = 'domain.com'">
  Redirect 301 "/" "http://www.domain.com/"
</If>

其他回答

<VirtualHost *:80>
    ServerAlias example.com
    RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>

如果你只想加载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>

我也有同样的问题。 但是用这个解决了

RewriteEngine上 重写HTTP_HOST} ! RewriteRule ^ (. *) http://www.% {HTTP_HOST} / 1美元(R = 301 L)

将non-www重定向到www。

这个规则将www重定向到非www

RewriteEngine上 ^my-domain\.com$ [NC] RewriteRule ^ (. *) http://my-domain.com/ 1美元(R = 301 L)

参考http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/

如果你正在使用Apache 2.4,不需要启用重写Apache模块,你可以使用这样的东西:

# non-www to www
<If "%{HTTP_HOST} = 'domain.com'">
  Redirect 301 "/" "http://www.domain.com/"
</If>

http://example.com/subdir/?lold=13666 => http://www.example.com/subdir/?lold=13666

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]