【问题标题】:Redirect non-www URL to www URL in conjunction with other rules结合其他规则将非www URL重定向到www URL
【发布时间】:2009-07-17 16:11:47
【问题描述】:

将点击http://example.com 的访问者重定向到http://www.example.com 并不难。但是如何与通过“index.php”引导所有页面请求的RewriteRule 结合使用?

RewriteRule !\.(gif|jpg|png|css|js|php|ico|xml)$ /index.php

【问题讨论】:

    标签: regex apache mod-rewrite


    【解决方案1】:

    您只需要确保那些导致外部重定向的规则出现在那些导致内部重写的规则之前。很简单:

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    RewriteRule !\.(gif|jpg|png|css|js|php|ico|xml)$ /index.php
    

    【讨论】:

      【解决方案2】:

      看答案for this post,反其道而行之。

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

      【讨论】:

      • 我只是在写类似的东西。我喜欢这种方式,因为从非 www 重定向根本不会干扰您的“正确”虚拟主机。
      猜你喜欢
      • 2011-06-24
      • 2019-03-09
      • 2011-11-05
      • 2013-12-20
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      相关资源
      最近更新 更多