2009-11-10 48 views
0

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

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

编辑:从你的描述的上方和下方的评论,这听起来像你想的请求是redirec如下所示:

 
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 

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

+0

您希望每个请求都重定向到index.php或index.html吗?你的意思是301重定向还是你的意思是翻译? – 2009-11-10 18:02:23

+0

我希望每个请求都被重定向。如果用户输入http://domain.com,他将被重定向到http://domain.com。如果用户输入http://www.domain.com,他将被重定向到http://domain.com/home/index.html。我的意思是301重定向。 – Bostjan 2009-11-10 18:53:44

+0

所以我把这段代码放到我的.htaccess文件中? – Bostjan 2009-11-10 20:44:04

回答

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] 
+0

Gumbo感谢您的回答,但如果我将这些行添加到我的.htaccess文件中,我的所有访问者都会被重定向到http://example.com/home/index.html目录。 我想重定向那些谁进入网址加www部分根目录如example.com http://www.example.com - > http://example.com/home/index.html 和 http://example.com - > http://example.com(其中index.php是) – Bostjan 2009-11-10 23:03:39

+0

好了,问题解决了。我需要的全部是 RewriteEngine on RewriteCond%{HTTP_HOST}^www \。 RewriteRule^http://example.com/home [R = 301,L] 谢谢!!!! – Bostjan 2009-11-11 13:24:46