htaccess如何重定向所有页面,但不重定向根目录



我需要帮助切换htaccess 301重定向。

我有一个网站www.site.com,但我需要将所有页面更改为www.newsite.com,但是我不想移动www.site.com(获取页面信息)

示例:

www.site.com (not move) index of to put then a template or message
www.site.com/2012/08/page.html move to www.newsite.com/2012/08/page.html
www.site.com/tag/keyword/ move to www.newsite.com/tag/keyword/
www.site.com/category/general/ move to www.newite.com/category/general/

我该怎么做?感谢

您可以尝试使用mod_alias的RedirectMatch在URI中强制执行重定向。在site.com文档根目录下的htaccess文件中,添加:

RedirectMatch 301 ^/(.+)$ http://www.newsite.com/$1

这将使得http://www.site.com/之后的任何内容都将被重定向,但只有http://www.site.com/不会。但是,http://www.site.com/index.html将被重定向。如果这是一个问题,您可以使用mod_rewrite:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/index.html$
# and whatever else you don't want redirected
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301]

www.site.com主机上,通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccessDOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?site.com$ [NC]
RewriteRule ^.+$ http://www.newsite.com%{REQUEST_URI} [L,R=301]

最新更新