首页 文章

apache从非www重定向到www

提问于
浏览
189

我有一个似乎没有从非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]

我错过了什么?

22 回答

  • 1

    使用重写引擎是解决此问题的一种非常重要的方法 . 这是一个更简单的解决方案:

    <VirtualHost *:80>
        ServerName example.com
        Redirect permanent / http://www.example.com/
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName www.example.com
        # real server configuration
    </VirtualHost>
    

    然后你将有另一个<VirtualHost>部分与 ServerName www.example.com 为您的真实服务器配置 . 当使用Redirect directive时,Apache会自动保留 / 之后的任何内容,这是一个常见的错误概念,说明为什么这个方法不起作用(事实上它确实如此) .

  • 478

    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]
    
  • 3
    <VirtualHost *:80>
        ServerAlias example.com
        RedirectMatch permanent ^/(.*) http://www.example.com/$1
    </VirtualHost>
    
  • 10

    要从 URL 网站中删除 www ,请在 .htaccess 文件中使用此代码:

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

    在您的网站中强制 www URL.htaccess 上使用此代码

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^YourSite.com$
    RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
    RewriteCond %{REQUEST_fileNAME} !-d
    RewriteCond %{REQUEST_fileNAME} !-f
    RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
    

    YourSite.com 必须用 URL 替换 .

  • 2
    <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 .

    干杯!

  • 0

    这类似于许多其他一些增强功能的建议:

    • 无需对域进行硬编码(适用于接受多个域或环境之间的vhost)

    • 保留方案(http / https)并忽略先前 %{REQUEST_URI} 规则的影响 .

    • 不受之前的 RewriteRule 影响的路径部分如 %{REQUEST_URI} .

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}/$1 [R=301,L]
    
  • 5
    RewriteCond %{HTTP_HOST} ^!example.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    

    这从 HTTP_HOST 变量开始,该变量仅包含传入URL的域名部分( example.com ) . 假设域名不包含 www. 并且与您的域名完全匹配,那么RewriteRule就会发挥作用 . 模式 ^(.*)$ 将匹配 REQUEST_URI 中的所有内容,这是HTTP请求中请求的资源( foo/blah/index.html ) . 它将它存储在后引用中,然后用于使用新域名重写URL(以 www 开头) .

    [NC] 表示不区分大小写的模式匹配, [R=301] 表示使用代码301的外部重定向(资源永久移动), [L] 停止所有进一步的重写,并立即重定向 .

  • 0

    我跑了这个......

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

    我需要在新服务器上对25个域进行通用,因此该指令位于<Directory>标记中的virtual.conf文件中 . (dir是所有docroots的父母)

    我不得不对重写规则进行一些修改,因为完整的docroot正在进行模式匹配,尽管http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html说它只是在主机和端口之后的东西 .

  • -1

    非www => www和对面www =>非www的重定向代码 . .htaccess文件中没有硬编码域和方案 . 因此将保留原始域和http / https版本 .

    APACHE 2.4和NEWER

    非WWW => WWW:

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

    WWW =>非WWW:

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

    注意:不在Apache 2.2上工作,其中%不可用 . 为了与Apache 2.2兼容,请使用下面的代码或将%替换为固定的http / https .


    APACHE 2.2和NEWER

    非WWW => WWW:

    RewriteEngine On
    
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    ...或更短的版本......

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|offs
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    WWW =>非WWW:

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

    ...短版本不可能,因为%N只能从最后一个RewriteCond获得...

  • -2

    将domain.tld重定向到www .

    可以在Apache指令或.htaccess文件中添加以下行:

    RewriteEngine on    
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    • 其他sudomains仍然有效 .

    • 无需调整线条 . 只需将它们复制/粘贴到正确的位置即可 .

    如果修改vhost,请不要忘记应用apache更改 .

    (基于默认的Drupal7 .htaccess但在很多情况下应该可以工作)

  • -1

    如果您使用的是Apache 2.4,而无需启用重写apache模块,则可以使用以下内容:

    # non-www to www
    <If "%{HTTP_HOST} = 'domain.com'">
      Redirect 301 "/" "http://www.domain.com/"
    </If>
    
  • 0
    <VirtualHost *:80>
        ServerAlias example.com
        RedirectMatch permanent ^/(.*) http://www.example.com/$1
    </VirtualHost>
    

    这将不仅重定向域名,还重定向内页 . 如...

    example.com/abcd.html ==> www.example.com/abcd.html
    example.com/ab/cd.html?ef=gh ==> www.example.com/ab/cd.html?ef=gh

  • 3

    试试这个:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com$ [NC]
    RewriteRule ^(.*) http://www.example.com$1 [R=301]
    
  • 5

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

    检查这完美的工作

  • 75

    Do not allways use Redirect permanent (or why it may cause issues)

    如果您以后可能会添加子域名,请不要使用 redirect permanent .

    因为如果客户使用了未注册为 VirtualHost 的子域,即使以后再注册,他也可能永远不会到达此子域 .

    redirect permanent 向客户端(浏览器)发送 HTTP 301 Moved Permanently ,并且其中许多人永远缓存此响应(直到[手动]清除缓存) . 因此,使用该子域将始终自动转发到www . ***,而无需再次请求服务器 .

    How long do browsers cache HTTP 301s?

    So just use Redirect

    <VirtualHost *:80>
      ServerName example.com
    
      Redirect / http://www.example.com/
    </VirtualHost>
    

    Apache.org - When not to use mod_rewrite

    Apache.org - Canonical Hostnames

  • 2

    这很简单!

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

    我刚遇到同样的问题 . 但是用这个解决了

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

    此规则将非www重定向到www .

    而这条规则将www重定向到非www

    RewriteEngine On RewriteCond %{HTTP_HOST} !^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/

  • 30

    这对我有用:

    RewriteCond %{HTTP_HOST} ^(?!www.domain.com).*$ [NC]
    RewriteRule ^(.*)$  http://www.domain.com$1  [R=301,L]
    

    我将前瞻模式 (?!www.domain.com) 用于在将所有域重定向到 www 子域时排除 www 子域,以避免Apache中的无限重定向循环 .

  • 15

    我使用的代码是:

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

    如果使用两个 <VirtualHost *:80> 块的上述解决方案与不同的 ServerName s ...

    <VirtualHost *:80>
        ServerName example.com
        Redirect permanent / http://www.example.com/
    </VirtualHost>
    <VirtualHost *:80>
        ServerName www.example.com
    </VirtualHost>
    

    ...那么你 must set NameVirtualHost On as well .

    如果您不允许自己使用不同的 ServerName 来区分块,那么您会收到以下错误消息:

    [warn] _default_ VirtualHost overlap on port 80, the first has precedence
    

    ...并且没有重定向发生,或者你有一个无限的重定向循环,这取决于你先放置哪个块 .

  • 43

    我在WP Multisite上有类似的任务,其中重定向规则必须是通用的(对于我添加到网络的任何给定域) . 我解决了首先在域中添加通配符(停放域) . 请注意 . 之后.com .

    CNAME * domain.com.
    

    然后我将以下行添加到我的多站点根目录下的.htaccess文件中 . 我想它适用于任何网站 .

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

    希望这会有所帮助 .

    PS . 如果您想从非www重定向到www,请将最后一行更改为

    RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
    
  • 1

    我发现在使用多个虚拟主机时使用ServerAlias更容易(也更有用) .

    <VirtualHost x.x.x.x:80>
        ServerName www.example.com
        ServerAlias example.com
        ....
    </VirtualHost>
    

    这也适用于https vhosts .

相关问题