URL重定向添加到.htaccess

时间:2009-11-10 17:02:47

标签: apache .htaccess mod-rewrite

任何人都可以帮我将重定向规则添加到我的.htaccess吗?我想根据用户输入的网址重定向网站访问者。例如:

  1. 如果访问者输入的网址没有www ,则会重定向到我的根目录(index.php)
  2. 如果访问者输入了网址,但将WWW 重定向到http://domain.com/home/index.html

  3. 修改:从上面的说明和下面的评论中,您可能希望将请求重定向为:

    www.domain.com             -> domain.com/home/index.html
    www.domain.com/about.php   -> domain.com/home/index.html
    domain.com                 -> domain.com/index.php
    domain.com/home/index.html -> domain.com/index.php
    domain.com/news.php?id=5   -> domain.com/index.php
    

    如果没有,请用一些更正的示例替换此编辑。

1 个答案:

答案 0 :(得分:1)

请尝试以下规则:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://example.com/home/index.html [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ http://example.com/index.php [R=301,L]

但请注意,如果您要重定向到同一主机(就像第一个规则可能那样),您将获得无限递归。因此,您可能希望通过排除/home/index.html

来划分第一条规则
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule !^home/index\.html$ http://example.com/home/index.html [R=301,L]