【问题标题】:Adding "www" with mod_rewrite [duplicate]用 mod_rewrite 添加“www” [重复]
【发布时间】:2011-11-30 00:54:50
【问题描述】:

可能重复:
301 redirect non-www to www not always working

我有一个网站,可以使用“www”和不使用“www”访问它(例如 www.example.comexample.com )。如果 URL 中没有“www”,则 mod_rewrite 用于重定向到 www.example.com。

但是,如果我去 example.com/something.html,它会被重定向到 www.example.comsomething.html 被截断)。

目前,我的规则是:

<VirtualHost *:80>
    ServerName example.com
    RewriteEngine On
    RewriteRule /.* http://www.example.com [R]
</VirtualHost>

使重写递归和使 example.com/something.html 重定向到 www.example.com/something.html 的最简单方法是什么

【问题讨论】:

  • 很难相信以前没有人问过这个问题.....(提示:搜索mod_rewrite prepend www

标签: apache mod-rewrite web no-www


【解决方案1】:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2013-07-09
    • 2015-03-11
    相关资源
    最近更新 更多