【问题标题】:URL redirection added to .htaccessURL 重定向添加到 .htaccess
【发布时间】:2010-12-15 03:16:20
【问题描述】:

谁能帮我将重定向规则添加到我的 .htaccess 中?我想根据他们输入的 URL 重定向网站访问者。例如:

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

编辑:根据您上面的描述和下面的评论,听起来您希望请求像这样被重定向:

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

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

【问题讨论】:

  • 您希望每个请求都重定向到 index.php 或 index.html?你是指 301 重定向还是翻译?
  • 我希望每个请求都被重定向。如果用户输入domain.com,他将被重定向到domain.com。如果用户输入domain.com,他将被重定向到domain.com/home/index.html。我的意思是 301 重定向。
  • 所以我把这段代码放到我的 .htaccess 文件中?
  • 不,我正在尝试了解您想要的行为。左边显示浏览器试图去的地方,右边是用户将被重定向的地方。这不可能是正确的,但这是你所指出的。请说明你想要什么。
  • 好的,这是我想要的方向:www.domain.com -> domain.com/home/index.html domain.com -> domain.com (index.php - 仅根目录中的索引!)

标签: apache .htaccess mod-rewrite


【解决方案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]

【讨论】:

  • Gumbo 感谢您的回答,但如果我将此行添加到我的 .htaccess 文件中,我的所有访问者都会被重定向到 example.com/home/index.html 目录。我想将那些在没有 www 部分的情况下输入 url 的人重定向到根目录,例如 example.com example.com --> example.com/home/index.htmlexample.com --> example.com (其中 index.php 是)
  • 好的,问题解决了。我所需要的只是 RewriteCond %{HTTP_HOST} ^www\ 上的 RewriteEngine。 RewriteRule ^ example.com/home [R=301,L] 谢谢!!!!
猜你喜欢
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 2013-07-06
  • 2017-05-12
  • 2013-01-09
相关资源
最近更新 更多